一、效果一
1.1界面如下
1.2 代码
private void NoPermissionsHintDialog() {
AlertDialog.Builder alert_builder = new AlertDialog.Builder(m_context);
alert_builder.setTitle("Warning")
.setIcon(R.drawable.warning_icon)
.setMessage("Please grant permissions firstly!")
//.setView(inputServer)
//.setView(mView)
.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
exit(0);
}
})
.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG,"m_JumpPermissionManagement");
}
});
alert_builder.setCancelable(false);
//alert_builder.show();
AlertDialog dialog = alert_builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setAllCaps(false);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setAllCaps(false);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
}
1.3 代码解析,下面这一行是点击对话框外面的时候不退出。
alert_builder.setCancelable(false);
1.4 代码解析,下面这两行是不让两个按钮显示的字符都是大写,没有这两行,显示都是大写。
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setAllCaps(false);
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setAllCaps(false);
1.5 代码解析,下面这两行是让按钮改变颜色
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
二、效果二,带输入框
2.1 效果图
2.2 代码
final EditText inputServer = new EditText(m_context);
View mView = LayoutInflater.from(m_context).inflate(R.layout.enterpassword, null);
final TextView m_password_string = (TextView) mView.findViewById(R.id.password_string);
final EditText input_password = (EditText) mView.findViewById(R.id.password);
m_password_string.setText("Enter the password for \"" + ssid + "\"");
AlertDialog.Builder alert_builder = new AlertDialog.Builder(m_context);
// builder.setTitle("Enter the password for \"" + ssid+ "\"")
alert_builder.setTitle("Settings")
.setIcon(R.drawable.setting)
//.setView(inputServer)
.setView(mView)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("Join", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (m_updatenetstatusthread.isAlive()) {
m_updatenetstatusthread.interrupt();
}
String m_password = input_password.getText().toString();
m_wifi_list_loading_icon.setVisibility(View.VISIBLE);
handler.sendEmptyMessageDelayed(BLE_SET_WIFI_PASSWORD_TIMEOUT_MESSAGE, 20000);
sendCmdWifiSetup(ssid, m_password, "123");
builder = new StringBuilder();
}
});
// builder.show();
alert_builder.setCancelable(false);
AlertDialog dialog = alert_builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
try {
//获取mAlert对象
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object mAlertController = mAlert.get(dialog);
//获取mTitleView并设置大小颜色
Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
mTitle.setAccessible(true);
TextView mTitleView = (TextView) mTitle.get(mAlertController);
//mTitleView.setTextSize(40);
mTitleView.setTextColor(getResources().getColor(R.color.colorPrimary));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
2.3 Android实现点击AlertDialog上按钮时不关闭对话框的方法,先检查输入的值是否有效,先setPositiveButton("Join", null);然后再getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener。
2.4 enterpassword 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/password_string"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Password "
android:textColor="@color/colorPrimary"
android:textSize="15dp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="82255222"
android:textSize="15dp"
android:textColor="#333333"
android:padding="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:digits="@string/rule_pwd"
android:hint="Password"
android:background="@drawable/tv_bg"/>
</LinearLayout>
三、参考文章
Android实现点击AlertDialog上按钮时不关闭对话框的方法 - 建站教程
android 带EditView(编辑框)的AlertDialog(对话框)及获取输入内容_editview获取文字_非琴不是筝的博客-CSDN博客
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色_alertdialog 字体大小_yechaoa的博客-CSDN博客