Android radio Button dialog box usage Example analysis _android

Source: Internet
Author: User

This example describes the use of the Android Radio button dialog box. Share to everyone for your reference. Specifically as follows:

Main.xml Layout file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "android:layout_width=" fill_parent "
  android:layout_height=" Fill_parent ">
  <edittext android:text=" " 
    android:id=" @+id/edittext "
    android:layout_width=" fill _parent "
    android:layout_height=" wrap_content " 
    android:editable=" false "
    android:cursorvisible=" False "/>
  <button android:text=" Displays the Radio dialog box " 
    android:id=" @+id/button "
    android:layout_width=" Fill_parent "
    android:layout_height=" wrap_content "/>
</LinearLayout>

Array.xml Array

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <string-array name= "hobby" >
   < item> Swimming </item>
   <item> playing basketball </item>
   <item> mountaineering </item>
 </string-array >
</resources>

Alertdialog class

Package com.ljq.dialog;
Import android.app.Activity;
Import Android.app.Dialog;
Import Android.app.AlertDialog.Builder;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
  public class Alertdialog extends activity {private EditText edittext;
  Private final static int dialog=1;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    edittext= (EditText) Findviewbyid (R.id.edittext);
    Button button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//Display dialog box Showdi
      Alog (DIALOG);
  }
    });
    /** * Create radio button dialog box */@Override protected Dialog oncreatedialog (int id) {Dialog dialog=null; Switch (ID) {Case Dialog:builDer builder=new Android.app.AlertDialog.Builder (this);
      Set the icon for the dialog box Builder.seticon (R.drawable.header);
      Sets the title Builder.settitle (radio button dialog box) for the dialog box; 0: The default first radio button is selected Builder.setsinglechoiceitems (r.array.hobby, 0, New Onclicklistener () {public void OnClick (
          Dialoginterface dialog, int which) {String hoddy=getresources (). Getstringarray (R.array.hobby) [which];
        Edittext.settext ("You selected:" +hoddy);
      }
      }); Add a OK button Builder.setpositivebutton (OK), new Dialoginterface.onclicklistener () {public void OnClick (Dia
      Loginterface dialog, int which) {}});
      Create a radio button dialog box Dialog=builder.create ();
    Break
  return dialog; }
}

Run Result:

I hope this article will help you with your Android program.

Related Article

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.