国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程實現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能示例

2019-10-22 18:27:13
字體:
供稿:網(wǎng)友

本文實例講述了Android編程實現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog。分享給大家供大家參考,具體如下:

帶有單選按鈕的dialog:

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 {  //聲明選中項變量  private int selectedCityIndex = 0;  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //定義城市數(shù)組    final String[] arrayCity = new String[] { "杭州", "紐約", "威尼斯", "北海道" };    //實例化AlertDialog對話框    Dialog alertDialog = new AlertDialog.Builder(this)        .setTitle("你最喜歡哪個地方?")            //設(shè)置標(biāo)題        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)        //設(shè)置對話框顯示一個單選List,指定默認(rèn)選中項,同時設(shè)置監(jiān)聽事件處理        .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            selectedCityIndex = which;        //選中項的索引保存到選中項變量          }        })        //添加取消按鈕并增加監(jiān)聽處理        .setNegativeButton("取消", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub          }        })        //添加確定按鈕并增加監(jiān)聽處理        .setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            Toast.makeText(getApplication(), arrayCity[selectedCityIndex], Toast.LENGTH_SHORT).show();          }        })        .create();    alertDialog.show();  }}

帶有復(fù)選按鈕的dialog代碼:

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);    //定義運動數(shù)組    final String[] arraySport = new String[] { "足球", "籃球", "網(wǎng)球", "乒乓球" };    final boolean[] arraySportSelected = new boolean[] {false, false, false, false};    //實例化AlertDialog對話框    Dialog alertDialog = new AlertDialog.Builder(this)        .setTitle("你喜歡哪些運動?")            //設(shè)置標(biāo)題        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)        //設(shè)置對話框顯示一個復(fù)選List,指定默認(rèn)選中項,同時設(shè)置監(jiān)聽事件處理        .setMultiChoiceItems(arraySport, arraySportSelected,            new DialogInterface.OnMultiChoiceClickListener() {          @Override          public void onClick(DialogInterface dialog, int which, boolean isChecked) {            arraySportSelected[which] = isChecked;       //選中項的布爾真假保存到選中項變量          }        })        //添加取消按鈕并增加監(jiān)聽處理        .setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            StringBuilder stringBuilder = new StringBuilder();            for (int i = 0; i < arraySportSelected.length; i++) {              if (arraySportSelected[i] == true){                stringBuilder.append(arraySport[i] + "、");              }            }            Toast.makeText(getApplication(), stringBuilder.toString(), Toast.LENGTH_SHORT).show();          }        })        //添加確定按鈕并增加監(jiān)聽處理        .setNegativeButton("取消", new DialogInterface.OnClickListener() {          @Override          public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub          }        })        .create();    alertDialog.show();  }}

希望本文所述對大家Android程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汉寿县| 绥德县| 陇南市| 通化县| 延长县| 福建省| 大理市| 界首市| 本溪| 方正县| 田阳县| 竹北市| 伊吾县| 商丘市| 洪江市| 栾川县| 扶余县| 班玛县| 永城市| 安多县| 盘锦市| 平果县| 阿克苏市| 昌图县| 南乐县| 河南省| 钦州市| 竹溪县| 鹰潭市| 江都市| 永昌县| 达拉特旗| 嘉定区| 淳化县| 苍南县| 新蔡县| 徐汇区| 怀仁县| 松桃| 清苑县| 龙口市|