由于开发中经常使用弹框,然而系统自带的弹框太局限,也不太美观,经常不能满足开发需求,所以就只能自定义布局。其实自定义布局很简单,没不要写出来,但是如果不写一遍的,后面遇到的话就感觉又会忘记,所以在次记一小笔,仅记一个最简单的例子,可以举一反三。
直接上代码:
dialog_setting_wifi_pwd.xml
<?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"
android:background="@color/Transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:background="@drawable/dialog_white_bg"
android:orientation="vertical"
android:padding="5dp">
<AutoCompleteTextView
android:id="@+id/dialog_setting_first_input_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:drawableBottom="@drawable/line_bottom"
android:gravity="center_vertical"
android:hint="请输入新密码"
android:padding="5dp"
android:textColor="@color/black"
android:textColorHint="@color/graywhite"
android:textSize="20sp" />
<AutoCompleteTextView
android:id="@+id/dialog_setting_second_input_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@null"
android:drawableBottom="@drawable/line_bottom"
android:gravity="center_vertical"
android:hint="请输入确认密码"
android:padding="5dp"
android:textColor="@color/black"
android:textColorHint="@color/graywhite"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/dialog_setting_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/btn_sure_nor"
android:text="@string/sure"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/dialog_setting_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/btn_cancel_nor"
android:text="@string/cancle"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
其中背景: dialog_white_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- android:radius 弧形的半径 -->
<corners android:radius="15dp" />
<!-- 填充的颜色 -->
<solid android:color="@android:color/white" />
</shape>
Activity代码中使用:
textView_camera_info_pwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final AlertDialog.Builder editDialog = new AlertDialog.Builder(MenuActivity.this);
LayoutInflater inflater = LayoutInflater.from(MenuActivity.this);
View v = inflater.inflate(R.layout.dialog_setting_wifi_pwd, null);
final AutoCompleteTextView firstPwd = v.findViewById(R.id.dialog_setting_first_input_pwd);
final AutoCompleteTextView secondPwd = v.findViewById(R.id.dialog_setting_second_input_pwd);
Button btnSure = v.findViewById(R.id.dialog_setting_sure);
Button btnCancle = v.findViewById(R.id.dialog_setting_cancle);
editDialog.setView(v);
editDialog.setCancelable(true);
final AlertDialog alertDialog = editDialog.create();
alertDialog.show();
btnSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setLoading(true);
NVTKitModel.removeWifiEventListener();
new Thread(new Runnable() {
@Override
public void run() {
if (!TextUtils.isEmpty(firstPwd.getText().toString())) {
if (firstPwd.getText().toString().equals(secondPwd.getText().toString())) {
String result = NVTKitModel.netSetPassword("" + secondPwd.getText());
if (result == null) {
Log.e(TAG, "set_passphrase fail");
}
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
checkDevDialog();
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MenuActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MenuActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
}
});
}
setLoading(false);
// checkDevDialog();
}
}).start();
}
});
btnCancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
});
文章参考于:https://blog.csdn.net/u010694658/article/details/53022294