(1)點選button呼叫AlertDialog
(2)將陣列的值放值AlertDialog中並將勾選的,顯示出來。
package COM.TQC.GDD01;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GDD01 extends Activity {
private Button button1;
private TextView text1;
private String[] s1={"美味蟹堡","義式香腸堡","蔬菜水果堡","香蕉潛艇堡","香烤雞肉堡"};
private boolean[] chsBool={false,false,false,false,false,false}; //先將checkbox值設為false
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview(); //宣告物件
setlistner();
}
private void findview(){
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.button1);
text1 = (TextView) findViewById(R.id.text1);
}
private void setlistner() {
// TODO Auto-generated method stub
button1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
setdialog();
}
});
}
private void setdialog()
{
AlertDialog dialog=new AlertDialog.Builder(GDD01.this)
//.setIcon(null)//設定無圖示
.setTitle(getResources().getString(R.string.str2))
.setMultiChoiceItems(s1, chsBool, new OnMultiChoiceClickListener()
{
public void onClick(DialogInterface dialog, int which, boolean isChecked)
{
}
})
.setPositiveButton("確認", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String selectedStr = "";
for(int _i=0;_i< chsBool.length;_i++)
{
if(chsBool[_i] == true)
{
selectedStr=selectedStr+"\n"+s1[_i].toString(); //換行動作
}
}
text1.setText("您點的餐有:"+selectedStr+"\n");
}
})
.setNegativeButton("離開", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
dialog.show();
}
}
沒有留言:
張貼留言