1. Achieve the effect:
Original interface: After clicking the button, display the dialog box after clicking the OK button after the dialog box, display a paragraph of text
Layout file:
<?xml version= "1.0" encoding= "Utf-8"?>
<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= "Com.example.dialoginterface.MainActivity" >
<textview
android:layout_margintop= "30DP"
android:layout_marginleft= "50DP"
Android:textsize= "30SP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>
<button
android:layout_marginleft= "50DP"
Android:id= "@+id/button"
android:text= "@string/str_button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
Android:textsize= "30SP"
android:layout_marginleft= "50DP"
Android:id= "@+id/text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
Java files:
public class Mainactivity extends Activity {
Private Button BT;
Private TextView TV;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
bt= (Button) Findviewbyid (R.id.button);
tv= (TextView) Findviewbyid (R.id.text);
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Create a dialog box object
Alertdialog.builder builder=new Alertdialog.builder (mainactivity.this);
Defining the contents of a dialog box
Builder.settitle (r.string.app_about);
Builder.setmessage (R.STRING.APP_ABOUT_MSG);
Define a Click event for the contents of the dialog box, and note that there is a show behind it, otherwise the dialog box will not appear
Builder.setpositivebutton (R.STRING.STR_OK, New Dialoginterface.onclicklistener () {
@Override
Define the actions to be performed after clicking the dialog button, here is adding a textview content
public void OnClick (Dialoginterface dialog, int which) {
Tv.settext ("This is the content that will only be displayed after clicking the dialog button!") ");
}
}). Show ();
}
});
}
}
Android Simple dialog box implementation