自定义 Android AlertDialog 对话框

自定义样式


EditText边框样式 bg_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <stroke
        android:width="1dp"
        android:color="#999999"></stroke>
<corners
    android:radius="10dp">
</corners>
</shape>
Button 按扭样式 bg_btn2.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_pressed="true">
    <shape>
        <solid android:color="#ff0000"></solid>
        <corners android:radius="10dp"></corners>
    </shape>
</item>
    <item android:state_pressed="false">
<shape>
    <solid android:color="#00ff00"></solid>
    <corners android:radius="10dp"></corners>
</shape>
    </item>
</selector>
自定义登入界面样式activity_login.xml
<?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"
    android:padding="10dp"
    >
<EditText
    android:id="@+id/userName"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:paddingLeft="10dp"
    android:hint="帐号"
    android:background="@drawable/bg_edit"/>
    <EditText
        android:id="@+id/passWord"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="10dp"
        android:layout_marginTop="10dp"
        android:hint="密码"
        android:background="@drawable/bg_edit"/>
    <Button
        android:id="@+id/Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_btn2"
        android:text="登入"/>
</LinearLayout>


后端核心代码
final View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.activity_login, null);
final EditText userName = view.findViewById(R.id.userName);
EditText passWord = view.findViewById(R.id.passWord);
Button btn = view.findViewById(R.id.Login);
final AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
builder4.setTitle("登入").setView(view).show();
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //获取登入框帐号userName.getText().toString() 密码  passWord.getText().toString()
        //这里可以做一些登入的逻辑代码
        //Toast.makeText(DialogActivity.this,array3[which],Toast.LENGTH_LONG);
    }
});

评论