Android Simple dialog box implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.