目录
1.Dialog
2.单选对话框
1.Dialog引入的几个头文件!
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
package com.example.dialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("退出提示");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("你确定要退出吗?");
//确定退出
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //退出对话框
MainActivity.this.finish(); //退出界面
}
});
//取消退出
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //退出对话框
}
});
AlertDialog alertDialog = builder.create(); //创建界面
alertDialog.show();//显示界面
}
}
2.单选对话框(改变字体大小)
引入的头文件
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
activity_main.xml
MainActivity.java
package com.example.dialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView textView;
private Button button;
private int index=0; //下标
private int[] textSize = new int[]{10,20,30,40,50}; //字体大小
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv);
button = findViewById(R.id.bt);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("字体大小");
builder.setIcon(R.mipmap.ic_launcher);
//设置单选列表 ①字符列表 ②选中的编号 ③接口
builder.setSingleChoiceItems(new String[]{"10", "20", "30", "40", "50"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { //which 选中哪一行的下标(从0开始)
index=which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setTextSize(textSize[index]);
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//生成一个对话框
AlertDialog alertDialog = builder.create(); //创建界面
alertDialog.show();//显示界面
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("退出提示");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("你确定要退出吗?");
//确定退出
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //退出对话框
MainActivity.this.finish(); //退出界面
}
});
//取消退出
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //退出对话框
}
});
AlertDialog alertDialog = builder.create(); //创建界面
alertDialog.show();//显示界面
}
}
作者:在路上Ven