Home > Mobile >  A custom dialog box display box (beginner)
A custom dialog box display box (beginner)

Time:10-09

1. Using linear layout set a simple button
 & lt; LinearLayout 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"

Android: orientation="vertical"
Tools: context="MainActivity" & gt;

Android: layout_width="wrap_content"
Android: layout_height="wrap_content"
Android: text="display dialog"
Android: layout_gravity="center_horizontal"
The android: onClick="click"
/>

Android: layout_width="wrap_content"
Android: layout_height="wrap_content"
Android: text="display radio button dialog"
Android: layout_gravity="center_horizontal"
The android: onClick="singleclick"
/>




2. Write Java code (MainActivity. Java) file:
 
Package com. Example. Dialogtest;

import android.os.Bundle;
The import android. The annotation. SuppressLint;
The import android. App. The Activity;
The import android. App. AlertDialog;
Import android. Content. DialogInterface.
Import android. Content. DialogInterface. An OnClickListener;
The import android. Text. AlteredCharSequence;
The import android. View. The Menu;
The import android. View. The view;
The import android. Widget. Toast;

Public class MainActivity extends the Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
setContentView(R.layout.activity_main);
}

Public void singleclick (View v) {
//1, to create AlertDialog. Builder object, Builder class is AlertDi alog the inner class
AlertDialog. Builder Builder=new AlertDialog. Builder (this);
//2. Call AlertDialog. Builder method for dialog box Settings icon
Builder. SetIcon (r. drawable. Ic_launcher);//Settings icon
//create a radio button displays the contents of the
String [] the items={" boy ", "girl"};
//builder. SetSingleChoiceItems (irems, default choice which one, when click the event) to perform
Builder. SetSingleChoiceItems (items, 0, null);
//3, call AlertDialog. Builder the create () method to create AlertDialog dialog
AlertDialog dialog=builder. The create ();
AlertDialog//4, call show () method to display a dialog
Dialog. The show ();

}

Public void click View (v) {
//1, to create AlertDialog. Builder object, Builder class is AlertDi alog the inner class
AlertDialog. Builder Builder=new AlertDialog. Builder (this);
//2. Call AlertDialog. Builder method for dialog box Settings icon, the title, content
Builder. SetIcon (r. drawable. Ic_launcher);//Settings icon
Builder. SetTitle (" prompt dialog ");//set the title
Builder. SetMessage (" I am your little cute! ! ");//set the content

//set a "ok" button for dialog -- the second parameter indicates the current user to select "ok" to execute when the event
//when press the confirm execution onClick method in the class A of
Builder. SetPositiveButton (" sure, "the new A ());

//set a "cancel" button for dialog
Builder. SetNegativeButton (" cancel ", the new B ());

//3, call AlertDialog. Builder the create () method to create AlertDialog dialog
AlertDialog dialog=builder. The create ();
AlertDialog//4, call show () method to display a dialog
Dialog. The show ();

}

//create the listener to monitor user press the confirm button
@ SuppressLint (" ShowToast ")
Class A, implements an OnClickListener {

@ Override
Public void onClick (DialogInterface arg0, int arg1) {
//Toast Auto - generated method stub
//makeTest (this, to display the text information, information display length)
//the information time has two choices: 1. The Toast. LENGTH_SHORT 2. Toast. LENGTH_LONG
Toast Toast=Toast. MakeText (MainActivity. This, "you press the determined", Toast. LENGTH_SHORT);
Toast. The show ();
}
}

//create the listener to listen the user press the cancel button
@ SuppressLint (" ShowToast ")
Class B implements an OnClickListener {

@ Override
Public void onClick (DialogInterface arg0, int arg1) {
//Toast Auto - generated method stub
//makeTest (this, to display the text information, information display length)
//the information time has two choices: 1. The Toast. LENGTH_SHORT 2. Toast. LENGTH_LONG
Toast Toast=Toast. MakeText (MainActivity. This, you press the "cancel", Toast. LENGTH_SHORT);
//note: MainActivity. This, why do you want to add MainActivity --
Toast. The show ();
}

Class C implements an OnClickListener {

@ Override
//the second parameter in which a radio button click is
Public void onClick (DialogInterface dialog, int which) {
//TODO Auto - generated method stub
If (which==0) {
//makeTest (this, to display the text information, information display length)
//the information time has two choices: 1. The Toast. LENGTH_SHORT 2. Toast. LENGTH_LONG
Toast Toast=Toast. MakeText (MainActivity. This, you select the "male", Toast. LENGTH_SHORT);
Toast. The show ();
} else {
//makeTest (this, to display the text information, information display length)
//the information time has two choices: 1. The Toast. LENGTH_SHORT 2. Toast. LENGTH_LONG
Toast Toast=Toast. MakeText (MainActivity. This, you select the "female", Toast. LENGTH_SHORT);
Toast. The show ();
}
}

}
}

@ Override
Public Boolean onCreateOptionsMenu (Menu Menu) {
//to Inflate the menu; This adds the items to the action bar if it is present.
GetMenuInflater (). Inflate (R.m emu. Main, menu).
return true;
}

}

CodePudding user response:

Dialog recommended DialogFragment implementation
  • Related