Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能-創(chuàng)新互聯(lián)

這篇文章主要介紹“Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能”,在日常操作中,相信很多人在Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供珉田數(shù)據(jù)中心 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。

帶有單選按鈕的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 {
  //聲明選中項(xiàng)變量
  private int selectedCityIndex = 0;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //定義城市數(shù)組
    final String[] arrayCity = new String[] { "杭州", "紐約", "威尼斯", "北海道" };
    //實(shí)例化AlertDialog對(duì)話框
    Dialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("你最喜歡哪個(gè)地方?")            //設(shè)置標(biāo)題
        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)
        //設(shè)置對(duì)話框顯示一個(gè)單選List,指定默認(rèn)選中項(xiàng),同時(shí)設(shè)置監(jiān)聽事件處理
        .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            selectedCityIndex = which;        //選中項(xiàng)的索引保存到選中項(xiàng)變量
          }
        })
        //添加取消按鈕并增加監(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);
    //定義運(yùn)動(dòng)數(shù)組
    final String[] arraySport = new String[] { "足球", "籃球", "網(wǎng)球", "乒乓球" };
    final boolean[] arraySportSelected = new boolean[] {false, false, false, false};
    //實(shí)例化AlertDialog對(duì)話框
    Dialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("你喜歡哪些運(yùn)動(dòng)?")            //設(shè)置標(biāo)題
        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)
        //設(shè)置對(duì)話框顯示一個(gè)復(fù)選List,指定默認(rèn)選中項(xiàng),同時(shí)設(shè)置監(jiān)聽事件處理
        .setMultiChoiceItems(arraySport, arraySportSelected,
            new DialogInterface.OnMultiChoiceClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            arraySportSelected[which] = isChecked;       //選中項(xiàng)的布爾真假保存到選中項(xiàng)變量
          }
        })
        //添加取消按鈕并增加監(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();
  }
}

到此,關(guān)于“Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

網(wǎng)頁標(biāo)題:Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article14/dpeode.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、小程序開發(fā)、網(wǎng)頁設(shè)計(jì)公司、建站公司Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司