如何在 Android 上创建自定义对话框?

androidapps/applicationsmobile development更新于 2026/2/14 3:07:17

此示例演示了如何在 Android 中创建自定义消息。

步骤 1 − 在 Android Studio 中创建一个新项目,前往"文件"->"新建项目",并填写所有必需的详细信息以创建新项目。

步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="My Custom Message"
      android:textSize="22sp"
      android:textAlignment="center"
      android:layout_marginTop="30dp"
      android:id="@+id/myCustommessage"/>
   <Button
      android:layout_width="140dp"
      android:layout_height="80dp"
      android:layout_centerInParent="true"
      android:text="Show Message"
      android:backgroundTint="@color/colorPrimary"
      android:textSize="20dp"
      android:textColor="#ffffff"
      android:onClick="btn_showMessage" />
</RelativeLayout>

步骤 3 − 在项目中点击 res → 右键单击​​布局 → 选择新建 → 布局资源文件 → 将布局命名为"custom_dialog",在"根元素"中输入 Linear 布局,然后单击"确定"

步骤 4 − 将以下代码添加到 custom_dialog.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="15dp">
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Enter your Message"
      android:textSize="22sp"
      android:textAlignment="center" />
   <EditText
      android:id="@+id/txt_input"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:hint="Enter your Message"
      android:textSize="20sp" />
   <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="15dp">
      <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorPrimary"
         android:text="Cancel"
         android:textColor="#ffffff"
         android:textSize="18sp"
         android:layout_weight="1"
         android:id="@+id/btn_cancel"/>
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorPrimary"
         android:text="Okay"
         android:textColor="#ffffff"
         android:textSize="18sp"
         android:layout_weight="1"
         android:id="@+id/btn_okay"/>
   </LinearLayout>
</LinearLayout>

步骤 5 − 将以下代码添加到 src/MainActivity.java

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   TextView myCustomMessage;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      myCustomMessage = (TextView)findViewById(R.id.myCustommessage);
   }
   public void btn_showMessage(View view){
      final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
      View mView = getLayoutInflater().inflate(R.layout.custom_dialog,null);
      final EditText txt_inputText = (EditText)mView.findViewById(R.id.txt_input);
      Button btn_cancel = (Button)mView.findViewById(R.id.btn_cancel);
      Button btn_okay = (Button)mView.findViewById(R.id.btn_okay);
      alert.setView(mView);
      final AlertDialog alertDialog = alert.create();
      alertDialog.setCanceledOnTouchOutside(false);
      btn_cancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            alertDialog.dismiss();
         }
      });
      btn_okay.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myCustomMessage.setText(txt_inputText.getText().toString());
            alertDialog.dismiss();
         }
      });
      alertDialog.show();
   }
}

步骤 6 − 将以下代码添加到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

让我们尝试运行您的应用程序。假设您已将实际的 Android 移动设备连接到电脑。要从 Android Studio 运行该应用程序,请打开项目的某个活动文件,然后点击工具栏上的"运行"图标  。选择您的移动设备作为选项,然后勾选将显示默认屏幕的移动设备。


点击此处下载项目代码。


相关文章