TQC-103 設定AlertDialog
這是TQC的103題,
程式碼為自行撰寫,
僅供參考。
主要練習AlertDialog的建置,和其相關事件。
package COM.TQC.GDD01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistner();
}
public void findview()
{
button1 = (Button) findViewById(R.id.button1);
text1 = (TextView) findViewById(R.id.text1);
}
public void setlistner()
{
button1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
setdialog();
}
});
}
public void setdialog()
{
AlertDialog dialog=new AlertDialog.Builder(GDD01.this)
.setIcon(R.drawable.icon)
.setTitle("menu")
.setMultiChoiceItems(s1, chsBool, new OnMultiChoiceClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(GDD01.this, ""+(which+1)+"已點:"+isChecked,Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String selectedStr = "";
// TODO Auto-generated method stub
for(int _i=0;_i<chsBool.length;_i++)
{
if(chsBool[_i] == true)
{
selectedStr=selectedStr+" "+s1[_i].toString();
}
}
text1.setText(selectedStr);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
dialog.show();
}
}
自我LV~