本文實例講述了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è)計有所幫助。
新聞熱點
疑難解答
圖片精選