Android single-choice button dialog box

Source: Internet
Author: User

From: http://www.cnblogs.com/linjiqin/archive/2011/03/10/1980170.html

Android radio button dialog box

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;

Publicclass alertdialog extends activity {
Private edittext;
Privatefinalstaticint dialog = 1;

@ Override
Publicvoid 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 (){
Publicvoid 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 = 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 (){
Publicvoid 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 (){
Publicvoid onclick (dialoginterface dialog, int which ){

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

}

Running result

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;

Publicclass alertdialog extends activity {
Private edittext;
Privatefinalstaticint dialog = 1;

@ Override
Publicvoid 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 (){
Publicvoid 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 = 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 (){
Publicvoid 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 (){
Publicvoid onclick (dialoginterface dialog, int which ){

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

}

Running result

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.