最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶(hù)提供珉田數(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ì)話(huà)框
    Dialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("你最喜歡哪個(gè)地方?")            //設(shè)置標(biāo)題
        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)
        //設(shè)置對(duì)話(huà)框顯示一個(gè)單選List,指定默認(rèn)選中項(xiàng),同時(shí)設(shè)置監(jiān)聽(tīng)事件處理
        .setSingleChoiceItems(arrayCity, 0, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            selectedCityIndex = which;        //選中項(xiàng)的索引保存到選中項(xiàng)變量
          }
        })
        //添加取消按鈕并增加監(jiān)聽(tīng)處理
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
          }
        })
        //添加確定按鈕并增加監(jiān)聽(tīng)處理
        .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ì)話(huà)框
    Dialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("你喜歡哪些運(yùn)動(dòng)?")            //設(shè)置標(biāo)題
        .setIcon(R.mipmap.ic_launcher)        //設(shè)置圖標(biāo)
        //設(shè)置對(duì)話(huà)框顯示一個(gè)復(fù)選List,指定默認(rèn)選中項(xiàng),同時(shí)設(shè)置監(jiān)聽(tīng)事件處理
        .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)聽(tīng)處理
        .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)聽(tīng)處理
        .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ī)?lái)更多實(shí)用的文章!


新聞名稱(chēng):Android編程怎么實(shí)現(xiàn)帶有單選按鈕和復(fù)選按鈕的dialog功能-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://fisionsoft.com.cn/article/dpeode.html