android:对话框AlertDialog
往往都需要新建一个页面,在此是不需要新建新的Activity,只需要一个Layout布局页面。以下附上全部代码示例(该实例是一个引导页,其中涉及到数据存储的知识点,可以看我的其他博客)注意:在这里与我们平时拿控件的方法不同,一定要加上view. 再加上控件。activity_guide.xml:引导页layout文件。一般我们在自定义页面的时候不会都写在一个页面中,造成代码杂糅。setNegati
安卓对话框是的实现需要用到 AlertDialog.Builder 实例对象
之后我们就用这个对象的以下方法
下面列举几个常用的方法
setTitle():设置 标题 setview():设置 自定义页面(下面会细讲自定义页面) setPositiveButton():一般用于确定功能 setNegativeButton():一般用于取消功能 setOnShowListener():对话框出现 setOnCancelListener():对话框消失
show():用于展示对话框 注意:如果没有这句话你的对话框不会显示
一般我们在自定义页面的时候不会都写在一个页面中,造成代码杂糅
往往都需要新建一个页面,在此是不需要新建新的Activity,只需要一个Layout布局页面
这里简单写一个
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_ip"
android:layout_width="269dp"
android:layout_height="83dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="68dp"
android:ems="10"
android:gravity="center"
android:hint="请输入IP,例如192.168.1.1"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/ed_port"
android:layout_width="261dp"
android:layout_height="86dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="72dp"
android:ems="10"
android:gravity="center"
android:hint="请输入端口,例如8080"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
那么我们如何加入到AlertDialog中呢
通过使用 setview():设置 自定义页面
View view=LayoutInflater.from(IntroductoryActivity.this).inflate(R.layout.ipconfig,null); ip = view.findViewById(R.id.edit_ip); port = view.findViewById(R.id.edit_port); setview(view);
注意:在这里与我们平时拿控件的方法不同,一定要加上view. 再加上控件
否则会造成空指针报错
以下附上全部代码示例(该实例是一个引导页,其中涉及到数据存储的知识点,可以看我的其他博客)
MainActivity:默认代码,无任何功能
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity对应的Layout文件:同样为默认代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
GuideActivity :引导页主界面
package com.example.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
public class GuideActivity extends AppCompatActivity {
private ViewPager vp;
private List<ImageView> imageViews;
private int[] imgs = {R.drawable.y0, R.drawable.y1, R.drawable.y2, R.drawable.y3};
private Button btn, btn2;
private EditText ed_ip, ed_port;
private ImageView[] dotViews;
private GuideAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_guide);
vp = findViewById(R.id.guide_vp);
btn = findViewById(R.id.guide_btn);
btn2 = findViewById(R.id.guide_btn2);
//初始化图片
initImgs();
//初始化底部圆点指示器
initDots();
adapter = new GuideAdapter(imageViews);
vp.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("isFirst", "1");
editor.apply();
Intent intent = new Intent(GuideActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = LayoutInflater.from(GuideActivity.this).inflate(R.layout.ipconfig, null);
ed_ip = view.findViewById(R.id.ed_ip);
ed_port = view.findViewById(R.id.ed_port);
AlertDialog.Builder builder = new AlertDialog.Builder(GuideActivity.this);
SharedPreferences sp = getSharedPreferences("demo1", MODE_PRIVATE);
ed_ip.setText(sp.getString("ip", ""));
ed_port.setText(sp.getString("port", ""));
builder.setTitle("端口设置")
.setView(view)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("ip", ed_ip.getText().toString());
editor.putString("port", ed_port.getText().toString());
editor.apply();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(GuideActivity.this, "取消端口设置", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotViews.length; i++) {
if (position == i) {
dotViews[i].setImageResource(R.drawable.guide_selector);
} else {
dotViews[i].setImageResource(R.drawable.guide_white);
}
if (position == dotViews.length - 1) {
btn.setVisibility(View.VISIBLE);
btn2.setVisibility(View.VISIBLE);
} else {
btn.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初始化底部圆点指示器
*/
private void initDots() {
LinearLayout layout = findViewById(R.id.guide_ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
params.setMargins(10, 0, 10, 0);
dotViews = new ImageView[imgs.length];
for (int i = 0; i < imageViews.size(); i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageResource(R.drawable.guide_white);
if (i == 0) {
imageView.setImageResource(R.drawable.guide_selector);
} else {
imageView.setImageResource(R.drawable.guide_white);
}
dotViews[i] = imageView;
final int finalI = i;
dotViews[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vp.setCurrentItem(finalI);
}
});
layout.addView(imageView);
}
}
/**
* 初始化图片
*/
private void initImgs() {
ViewPager.LayoutParams params = new ViewPager.LayoutParams();
imageViews = new ArrayList<ImageView>();
for (int i = 0; i < imgs.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(params);
imageView.setImageResource(imgs[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageViews.add(imageView);
}
}
}
activity_guide.xml:引导页layout文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/guide_vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/guide_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:orientation="horizontal" />
<Button
android:id="@+id/guide_btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_above="@id/guide_ll"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#2990E3"
android:text="开始体验"
android:textColor="#ffffff"
android:visibility="invisible" />
<Button
android:id="@+id/guide_btn2"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignRight="@id/guide_vp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#2990E3"
android:text="端口设置"
android:textColor="#ffffff"
android:visibility="invisible"/>
</RelativeLayout>
GuideAdapter :引导页适配器
package com.example.myapplication;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;
public class GuideAdapter extends PagerAdapter {
private final List<ImageView> imageViews;
public GuideAdapter(List<ImageView> imageViews) {
this.imageViews= imageViews;
}
/**
* 获取当前要显示对象的数量
*/
@Override
public int getCount() {
return imageViews.size();
}
/**
* 判断是否用对象生成界面
*/
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view== o;
}
/**
* 从ViewGroup中移除当前对象
*/
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(imageViews.get(position));
}
/**
* 当前要显示的对象
*/
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(imageViews.get(position));
return imageViews.get(position);
}
}
ipconfig.xml:ip配置界面
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ed_ip"
android:layout_width="269dp"
android:layout_height="83dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="68dp"
android:ems="10"
android:gravity="center"
android:hint="请输入IP,例如192.168.1.1"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/ed_port"
android:layout_width="261dp"
android:layout_height="86dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="72dp"
android:ems="10"
android:gravity="center"
android:hint="请输入端口,例如8080"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
guide_white.xml:未选择白色圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="20dp" android:width="20dp"/>
<solid android:color="#fff"/>
</shape>
guide_selector.xml :选择蓝色圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="20dp" android:width="20dp"/>
<solid android:color="#0687ED"/>
</shape>
引导页的4张图片自己加就可以了
更多推荐
所有评论(0)