109-AlertDialog顯示結果(行動裝置應用)
1.GDD01類別程式碼
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
public class GDD01 extends Activity {
private EditText et;
private RadioButton rb1;
private RadioButton rb2;
private CheckBox cb1;
private CheckBox cb2;
private CheckBox cb3;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.name);
rb1 = (RadioButton) findViewById(R.id.rButton1);
rb2 = (RadioButton) findViewById(R.id.rButton2);
cb1 = (CheckBox) findViewById(R.id.cBox1);
cb2 = (CheckBox) findViewById(R.id.cBox2);
cb3 = (CheckBox) findViewById(R.id.cBox3);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String s = "";
if (et.getText().toString().equals("")) {
s += "姓名:未填\n";
} else {
s += "姓名:" + et.getText().toString() + "\n";
}
if (rb1.isChecked()) {
s += "性別:" + rb1.getText().toString() + "\n";
} else {
s += "性別:" + rb2.getText().toString() + "\n";
}
int count = 0;
s+="興趣:";
if (cb1.isChecked()) {
count++;
s +=cb1.getText().toString() + ",";
}
if (cb2.isChecked()) {
count++;
s +=cb2.getText().toString() + ",";
}
if (cb3.isChecked()) {
count++;
s +=cb3.getText().toString() + ",";
}
if (count == 0) {
s += "未選擇";
}
new AlertDialog.Builder(GDD01.this)
.setTitle("結果")
.setMessage(s)
.setPositiveButton("離開",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}}).show();
}
});
}
}
沒有留言:
張貼留言