一個簡單的表單 , 內含姓名 , 性別及興趣欄位 , 當使用者填寫完表單內的資料後 , 按下顯示結果按鈕 , 此時程是會收集表單內填寫的資訊 , 已跳出AlertDialg 的方式呈現表單輸入結果 , 這題只是單純的AlertDialog顯示我們選擇的資料 , 以下是程式碼。
package COM.TQC.GDD01;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
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;
private AlertDialog.Builder AB;
private String content;
@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);
AB = new AlertDialog.Builder(this);
AB.setTitle("結果");
AB.setPositiveButton("離開", new OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
// 作用為關閉AlertDialog
}
}
);
button.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
if(et.getText().toString().trim().length()==0) content = "姓名:未填\n";
//防止使用者鍵入空白 , 考試時使用 getText().equals("") 即可
else content = "姓名:"+et.getText()+"\n";
if(rb1.isChecked()) content+="性別:帥哥\n興趣:";
else content+="性別:美女\n興趣:";
if(cb1.isChecked()==false&&cb2.isChecked()==false&&cb3.isChecked()==false)
content+="未選擇";
else
{
if(cb1.isChecked()) content+="吃飯 ";
if(cb2.isChecked()) content+="睡覺 ";
if(cb3.isChecked()) content+="上網 ";
}
AB.setMessage(content).show();
}
});
}
}
以下是 main.xml
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....


沒有留言:
張貼留言