A custom dialog
Implementation steps:
1. Customize a class, inherit from the Dialog class, and call setContentView(R.layout.xx) in the constructor to set the layout of the dialog
2. Set the style of the custom dialog (do not display the title bar, do not display the background), because the Dialog is inherited, a column of the Dialog title is added by default
3. Find res-->values-->styles.xml to set the style
4. Invoke the constructor with the set dialog style parameters
5. Add events to some controls in the custom dialog
6. Instantiate a custom dialog and display
1. The layout file of the new dialog box (that is, the appearance of the pop-up dialog box)
layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:background="#FFFF00" android:padding="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tips" android:textSize="34sp" android:textStyle="bold" android:layout_marginTop="30dp"/> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#ffffff" android:layout_marginTop="10dp" android:layout_marginBottom="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Are you sure you want to exit the current program" android:layout_marginTop="10dp" android:textSize="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="否" android:onClick="onClick" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是" android:onClick="onClick" /> </LinearLayout> </LinearLayout>
2. Set the click button of the pop-up dialog box
activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:padding="10dp"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="custom dialog" android:onClick="onClick" /> </LinearLayout>
3. Set the style of the dialog box
Add the following code to style.xml
<style name="mydialog" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
Because the custom MyDialog is inherited from Dialog, the title column of the Dialog is added by default, which is very unsightly, so the style of the re-customized dialog is used. Set name="android:windowNoTitle" to true to represent no window title, android:windowBackground to represent the dialog background, and @android:color/transparent to use a transparent color.
The complete style file is in app-->res-->values-->styles.xml, the complete code is as follows:
styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="mydialog" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style> </resources>
Fourth, establish the MyDialog class to inherit the Dialog class to implement dialog management
MyDialog.java
package com.administrator.dialogtest; import android.app.Dialog; import android.content.Context; import android.support.annotation.NonNull; import android.view.View; import android.widget.Button; /** * Created by Administrator on 2018/5/7. */ public class MyDialog extends Dialog { public MyDialog(@NonNull Context context) { // super(context);//The constructor of the parent class that satisfies the child parameter form super(context, R.style.mydialog);//Call the parameter-containing constructor of the parent class to set the style to the style of mydialog setContentView(R.layout.layout);//Set the dialog box //Add a click event to the button in the custom Dialog Button yesButton = (Button)findViewById(R.id.btn2); Button noButton = (Button)findViewById(R.id.btn1); noButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss();//The control dialog box disappears } }); yesButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.exit(0); } }); } // //You can also directly rewrite the constructor with two parameters and pass in the style externally // public MyDialog(@NonNull Context context, int themeResId) { // super(context, themeResId); // setContentView(R.layout.layout); // } //When called externally, MyDialog dialog = new MyDialog(this,R.style.mydialog); //dialog.show(); }
5. Call in MainActivity
MainActivity.java
package com.administrator.dialogtest; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn1); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: //1. Customize a class, inherit from the Dialog class, and call setContentView(R.layout.xx) in the constructor to set the layout of the dialog //2. Set the style of the custom dialog box (do not display the title bar, do not display the background), because the Dialog is inherited, a column of the Dialog title is added by default //3. Find res-->values-->styles.xml to set the style //4. Call the constructor with the set dialog style parameters //5. Add events to some controls in the custom dialog //6. Instantiate a custom dialog box to display MyDialog dialog = new MyDialog(this); dialog.show(); break; } } }
2. Add adapter case
Create a new layout array_item_layout.xml as the resource data source referenced by the adapter
array_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" android:gravity="center_vertical"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/item_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" android:layout_marginLeft="15dp"/> </LinearLayout>
Add the click button layout that triggers the dialog in activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:padding="10dp"> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Adapter case" android:onClick="onClick" /> </LinearLayout>
Create an adapter in a Java file and call
MainActivity.java
package com.administrator.dialogtest; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); Button btn2 = findViewById(R.id.btn1); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn2: showArrayDialog (); break; } } private void showArrayDialog() { final String[] items = {"Java","Mysql","Android","Html","C","JavaScript"}; //array adapter //parameter 1: environment //Parameter 2: Layout resource index, which refers to the style android.R.layout.xxx presented by each item of data //parameter 3: data source // ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items); //parameter 4: data source //Parameter 3: int textViewId specifies that the text data source needs to be placed in the position of the corresponding id text control in the layout //parameter 2: layout resource index //parameter 1: environment ArrayAdapter adapter = new ArrayAdapter(this,R.layout.array_item_layout,R.id.item_txt,items); AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("Please select:") //Parameter 1: Adapter object (ruler for data display style) //parameter 2: listener .setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show(); dialog.dismiss();//After clicking, let the dialog box disappear } }); builder.show(); } }