In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize dialog function with radio button and check button in Android programming". In daily operation, it is believed that many people have doubts about how to realize dialog function with radio button and check button in Android programming. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how to realize the dialog function with radio button and check button in Android programming"! Next, please follow the editor to study!
Dialog with radio buttons:
Package example.com.myapplication;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity {/ / declare the selected variable private int selectedCityIndex = 0; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) / / define the city array final String [] arrayCity = new String [] {"Hangzhou", "New York", "Venice", "Hokkaido"}; / / instantiate the AlertDialog dialog Dialog alertDialog = new AlertDialog.Builder (this) .setTitle ("which place do you like best?") / / set title .setIcon (R.mipmap.ic_launcher) / / set icon / / Settings dialog box displays a radio List, specifying the default selection, and setting listening event handling .setSingleChoiceItems (arrayCity, 0, new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {selectedCityIndex = which) / / Save the index of the selected item to the selected variable}}) / / add the cancel button and add listening processing .setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog) Int which) {/ / TODO Auto-generated method stub}) / / add OK button and add listening processing .setPositiveButton ("confirm", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast.makeText (getApplication (), arrayCity [selectedCityIndex], Toast.LENGTH_SHORT). Show () }) .create (); alertDialog.show ();}}
Dialog code with a check button:
Package example.com.myapplication;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) / define the motion array final String [] arraySport = new String [] {"football", "basketball", "tennis", "table tennis"}; final boolean [] arraySportSelected = new boolean [] {false, false}; / / instantiate the AlertDialog dialog Dialog alertDialog = new AlertDialog.Builder (this) .setTitle ("which sports do you like?") / / set title .setIcon (R.mipmap.ic_launcher) / / set icon / / Settings dialog box displays a check List, specifies the default selection, and sets listening event handling .setMultiChoiceItems (arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener () {@ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {arraySportSelected [which] = isChecked / / add cancel button and add listening processing .setPositiveButton ("confirm", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {StringBuilder stringBuilder = new StringBuilder (); for (int I = 0; I < arraySportSelected.length) True +) {if (arraySportSelected [I] = = true) {stringBuilder.append (arraySport [I] + ",");}} Toast.makeText (getApplication (), stringBuilder.toString (), Toast.LENGTH_SHORT) .show () }) / / add OK button and add listener processing .setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {/ / TODO Auto-generated method stub}}) .create (); alertDialog.show () }} at this point, the study on "how to realize the dialog function with radio buttons and check buttons in Android programming" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.