Android Dialog Dialog Box Instance Code Explanation

  • 2021-10-13 08:48:42
  • OfStack

Basic Methods of Dialog


// Create Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Set the title icon 
builder.setIcon(R.drawable.ic_launcher);
// Set the title 
builder.setTitle(" This is 1 Dialog boxes ");
// Setting information 
builder.setMessage(" Do you want to jump? ");
// OK button 
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
// Cancel button 
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
// Ignore 
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
// Display Dialog Box 
show();

System style

1. Drop-down list


AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     builder.setIcon(R.drawable.ic_launcher);
     builder.setTitle(" Select 1 Cities ");
     // Data source for drop-down list 
     final String[] cities = {" Beijing ", " Shanghai ", " Guangzhou ", " Shenzhen ", " Hangzhou "};
     builder.setItems(cities, new DialogInterface.OnClickListener(){
       @Override
       public void onClick(DialogInterface dialog, int which){
         Toast.makeText(MainActivity.this, " The city you chose is: " + cities[which], Toast.LENGTH_SHORT).show();
       }
 });
 builder.show();

2. Radio Box List


AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle(" Please select a gender ");
    final String[] sex = {" Male ", " Female "};
    // No. 1 2 Parameters specify which radio box is checked by default 
    builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
          Toast.makeText(MainActivity.this, " Sex is: " + sex[which], Toast.LENGTH_SHORT).show();
      }
    });
    builder.setPositiveButton(" Determine ", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
    builder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
builder.show();

3. Multi-check box list


String str;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle(" Choose the TV series you want to watch ");
final String[] hobbies = {" Story of Tingxi ", " Soaring ", " Sweet honey is as heavy as frost ", " Ruyi\ 's Royal Love "};
// No. 1 2 Parameters represent which options are selected and need to be passed 1 A boolean[] Array, its length should be the same as the first 1 Parameters have the same length , If null Indicates that none of them are selected 
builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){
    @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked){
          if(isChecked){
                    str.append(hobbies[which] + ", ");
          }
          Toast.makeText(MainActivity.this, " The choice is: " + str.toString(), Toast.LENGTH_SHORT).show();
  }
 });
builder.setPositiveButton(" Determine ", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which){

  }
});
builder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener(){
        @Override
        public void onClick(DialogInterface dialog, int which){

  }
});
builder.show();

4. Wait for the dialog box


ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this);
waitingDialog.setTitle(" Waiting for loading, please wait ...");
waitingDialog.setMessage(" Waiting ...");
waitingDialog.setIndeterminate(true);// Progress bar with uncertainty 
waitingDialog.setCancelable(false);// Click External Do Not Cancel Dialog Box 
waitingDialog.show();

5. Progress Bar Dialog Box


int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);// Set default values 
progressDialog.setTitle(" Downloading ");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// Progress bar style 
progressDialog.setMax(MAXPD);// Set the maximum value 
progressDialog.show();

Custom Dialog

1. Inherit Dialog


public class CustomDialog extends Dialog {
// Title 
protected TextView hintTv;
// Left button 
protected Button doubleLeftBtn;
// Right button 
protected Button doubleRightBtn;
// Input box 
public EditText editText;
public CustomDialog(Context context) {
  super(context, R.style.CustomDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setCancelable(false); //  Can I undo it 
  setContentView(R.layout.dialog_custom);
  hintTv = findViewById(R.id.tv_common_dialog_hint);
  doubleLeftBtn = findViewById(R.id.btn_common_dialog_double_left);
  doubleRightBtn = findViewById(R.id.btn_common_dialog_double_right);
  editText = findViewById(R.id.et_common_dialog);
}
// Right-click text and click events 
public void setRightButton(String rightStr, View.OnClickListener clickListener) {
  doubleRightBtn.setOnClickListener(clickListener);
  doubleRightBtn.setText(rightStr);
}
// Set left-click text and click events 
public void setLeftButton(String leftStr, View.OnClickListener clickListener) {
  doubleLeftBtn.setOnClickListener(clickListener);
  doubleLeftBtn.setText(leftStr);
}
// Set prompt content 
public void setHintText(String str) {
  hintTv.setText(str);
  hintTv.setVisibility(View.VISIBLE);
}
// Give two buttons   Set text 
public void setBtnText(String leftStr, String rightStr) {
  doubleLeftBtn.setText(leftStr);
  doubleRightBtn.setText(rightStr);
}
}

2. Customize Style


<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog">
    <!--  Border  -->
    <item name="android:windowFrame">@null</item>
    <!--  Background transparency  -->
    <item name="android:windowBackground">@color/transparent</item>
    <!--  Untitled  -->
    <item name="android:windowNoTitle">true</item>
    <!--  Does it emerge in activity Above  -->
    <item name="android:windowIsFloating">true</item>
    <!--  Translucent  -->
    <item name="android:windowIsTranslucent">false</item>
    <!--  Background blur  -->
    <item name="android:windowContentOverlay">@null</item>
    <!--  Allow the background of the dialog box to dim  -->
    <item name="android:backgroundDimEnabled">true</item>
    <!--  Font color  -->
    <item name="android:textColor">@color/white</item>
    <item name="android:editTextColor">@color/white</item>
 </style>

3. Customize the layout


<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/ll_common_dialog_layout"
  android:layout_width="500dp"
  android:layout_height="250dp"
  android:layout_margin="5dp"
  android:background="@drawable/background_info"
  android:orientation="vertical"
  android:gravity="center">

  <!-- Prompt information -->
  <TextView
    android:id="@+id/tv_common_dialog_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="27sp"/>
  <EditText
    android:id="@+id/et_common_dialog"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:textColor="@color/back"
    android:inputType="text|textNoSuggestions"
    tools:ignore="LabelFor"
    android:hint=" Please enter your password "/>
  
  <!-- Bottom button -->
  <LinearLayout
    android:id="@+id/ll_common_dialog_double"
    android:layout_width="360dp"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <Button
      android:id="@+id/btn_common_dialog_double_left"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>

    <Button
      android:id="@+id/btn_common_dialog_double_right"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>
  </LinearLayout>
</LinearLayout>

4. ipad hides the bottom virtual key


// Eject dialog Hide the bottom virtual key when 
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
  @Override
  public void onSystemUiVisibilityChange(int visibility) {
    int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    if (Build.VERSION.SDK_INT >= 19) {
      uiOptions |= 0x00001000;
    } else {
      uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
    }
    dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
  }
});

5. Use custom Dialog


AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     builder.setIcon(R.drawable.ic_launcher);
     builder.setTitle(" Select 1 Cities ");
     // Data source for drop-down list 
     final String[] cities = {" Beijing ", " Shanghai ", " Guangzhou ", " Shenzhen ", " Hangzhou "};
     builder.setItems(cities, new DialogInterface.OnClickListener(){
       @Override
       public void onClick(DialogInterface dialog, int which){
         Toast.makeText(MainActivity.this, " The city you chose is: " + cities[which], Toast.LENGTH_SHORT).show();
       }
 });
 builder.show();
0

Summarize


Related articles: