Android radio button dialog box

Source: Internet
Author: User

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 = "show 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 = "holobby">
<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
ShowDialog (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 in the dialog box
Builder. setIcon (R. drawable. header );
// Set the title of the dialog box
Builder. setTitle ("single choice button dialog box ");
// 0: the first radio button is selected by default.
Builder. setSingleChoiceItems (R. array. holobby, 0, new OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
String hoddy = getResources (). getStringArray (R. array. Hoby) [which];
EditText. setText ("You selected:" + hoddy );
}
});

// Add a confirmation button
Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){

}
});
// Create a single choice button dialog box
Dialog = builder. create ();
Break;
}
Return dialog;
}

}

Running result

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.