AlertDialog對話方塊很像Windows上的彈跳視窗,在Android裡的AlertDialog,她不只是可以放上文字,還可以放上任何元件。
AlertDialog互動對話方塊基本語法有:
setTitle():對話方塊的標題
setIcon():對話方外的圖示
setMessage():對話方塊的內容
setItems():對話方塊的列表內容
setPositiveButton():在對話方塊中加入YES的按鈕
setNegativeButton():在對話方塊中加入NO的按鈕
setNeutralButton():在對話方塊中加入取消(Ignore)的按鈕
而setPositiveButton、setNegativeButton、setNeutralButton函式都可以用來定義按鈕,字面上的
意思是正面/中立/負面,但沒有特殊含義,為了程式設計師方便於區分。
首先,我們先來說說如何加入基本的按鈕:
//語法一:AlertDialog.Builder 變數名稱 = new AlertDialog.Builder(主程式類別);
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("基本訊息對話按鈕");
dialog.setMessage("基本訊息對話功能介紹");
dialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我還尚未了解",Toast.LENGTH_SHORT).show();
}
});
dialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "我了解了",Toast.LENGTH_SHORT).show();
}
});
dialog.setNeutralButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "取消",Toast.LENGTH_SHORT).show();
}
});
dialog.show();
第二、我們來討論在AlertDialog裡利用List來呈現:
接著,第二個處理事件,我們先建一個陣列,並且把字串丟進去AlertDialog,這樣它就會自動變成一個List
注意 : 列表不顯示無奈重寫了一次發現setMessage和setSingleChoiceItems不能同時使用。
final String[] dinner = {"腿庫","雞蛋糕","沙威瑪","澳美客","麵線","麵疙瘩"};
AlertDialog.Builder dialog_list = new AlertDialog.Builder(MainActivity.this);
dialog_list.setTitle("利用List呈現");
dialog_list.setItems(dinner, new DialogInterface.OnClickListener(){
@Override
//只要你在onClick處理事件內,使用which參數,就可以知道按下陣列裡的哪一個了
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你選的是" + dinner[which], Toast.LENGTH_SHORT).show();
}
});
dialog_list.show();
第三、最後一個是把寫好的Layout丟進來,首先我們要先準備好一個xml
XML檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<requestFocus />
</EditText>
</LinearLayout>
JAVA檔新增文字有:
//inflate目的是把自己設計xml的Layout轉成View,作用類似於findViewById,它用於一個沒有被載入或者想要動態
//載入的介面,當被載入Activity後才可以用findViewById來獲得其中界面的元素
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v = inflater.inflate(R.layout.alertdialog_use, null);
//語法一:new AlertDialog.Builder(主程式類別).XXX.XXX.XXX;
new AlertDialog.Builder(MainActivity.this)
.setTitle("請輸入你的id")
.setView(v)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText editText = (EditText) (v.findViewById(R.id.editText1));
Toast.makeText(getApplicationContext(), "你的id是" +
editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
})
.show();
顯示多選列表對話框setMultiChoiceItems
一、通过AlertDialog.Builder类的setMultiChoiceItems()方法可以創建列表對話框,方法如下:
1、 從資源文件中裝載數據:
public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, final OnMultiChoiceListener listener)
2、從數據集中裝載數據 :
public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, final OnMultiChoiceListener listener)
3、從字串數組中裝載數據 :
public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceListener listener)
EX1 :
package com.js.multichoiceitemstest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
importandroid.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MultiChoiceItemsTest extends Activity implements OnClickListener
{
private String[] province = new String[] { "上海", "北京", "湖南", "湖北", "海南" };
private Button btnMultiChoice;
private ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnMultiChoice = (Button) findViewById(R.id.btnMultiChoice);
btnMultiChoice.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
showMultiChoiceItems();
}
private void showMultiChoiceItems()
{
AlertDialog builder = newAlertDialog.Builder(this)
.setTitle("請選擇您的城市:")
.setMultiChoiceItems(province,
new boolean[] { false, false, false, false, false },
new OnMultiChoiceClickListener()
{
@Override
public voidonClick(DialogInterface dialog,
int which, booleanisChecked)
{
// TODO Auto-generated method stub
}
})
.setPositiveButton("確定", newDialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
String s = "您選擇了:";
// 掃描所有的列表項,如果當前列表項被選中,將列表項的文本追加到s變數中。
for (int i = 0; i < province.length; i++)
{
if(lv.getCheckedItemPositions().get(i))
{
s += i + ":" + lv.getAdapter().getItem(i) + " ";
}
}
// 用戶至少選擇了一個列表項
if(lv.getCheckedItemPositions().size() > 0)
{
newAlertDialog.Builder(MultiChoiceItemsTest.this)
.setMessage(s).show();
System.out.println(lv.getCheckedItemPositions().size());
}
// 用戶未選擇任何列表項
elseif(lv.getCheckedItemPositions().size() <= 0 )
{
newAlertDialog.Builder(MultiChoiceItemsTest.this)
.setMessage("您未選擇任何城市").show();
}
}
}).setNegativeButton("取消", null).create();
//
lv = builder.getListView();
builder.show();
}
}
EX2 :
package com.rrr.dia;
import Android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DiaActivity extends Activity {
/** Called when the activity is first created. */
private Button btn;
private TextView tv;
private String[] shuxing = { "字體顏色", "字體大小" };
private boolean[] chuzhi = { false, false };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Builder b = new AlertDialog.Builder(DiaActivity.this);
b.setTitle("gggggg");
b.setMultiChoiceItems(shuxing, chuzhi,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
switch (which) {
case 0:
chuzhi[0] = isChecked;
break;
case 1:
chuzhi[1] = isChecked;
break;
default:
break;
}
}
});
b.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (chuzhi[0] == true) {
tv.setTextColor(Color.BLUE);
}
if (chuzhi[1] == true) {
tv.setTextSize(50f);
}
}
});
b.create().show();
}
});
}
}
沒有留言:
張貼留言