The content area of a normal dialog box generally displays simple text information. It is set by calling the setMessage() method of the AlertDialog.Builder object,
1. Create a program
Create an application called Text062
2. Realize common dialog box through code
details as follows:
MainActivity.java
package com.example.text06; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed(){ AlertDialog dialog = null; AlertDialog finalDialog = dialog; AlertDialog finalDialog1 = dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("普通对话框") .setIcon(R.mipmap.ic_launcher) .setMessage("Are you sure to exit the application:") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int which) { finalDialog.dismiss (); MainActivity.this.finish(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override } }); dialog = builder.create(); public void onClick(DialogInterface dialogInterface, int which) { finalDialog1.dismiss(); dialog.show(); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
The result of the operation is as follows:
Well, the above is the content of learning.