TQC- 109 AlertDialog

  • 1714
  • 0

TQC- 109 AlertDialog

這是TQC的109題,

程式碼為自行撰寫,

僅供參考。

 

主要練習各元件判斷、AlertDialog

 


package COM.TQC.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;
  
  public String _int="";
  public String _sex;
  public String _mes="";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    findview();
    setlistener();
   
  }
  
  public void findview()
  {
	  	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);
  }
  
  public void setlistener()
  {
	  rb1.setOnClickListener(new RadioButton.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sex="M";
				rb2.setChecked(false);
			}});
	  rb2.setOnClickListener(new RadioButton.OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sex="G";
				rb1.setChecked(false);
			}});
		  
	  button.setOnClickListener(new Button.OnClickListener(){
	      @Override
	      public void onClick(View v){
	        settext();
	        openDialog();
	      }
	    });
  }
  
  public void settext()
  {
	  _mes="";
	  if(cb1.isChecked()) {  
		  _int += cb1.getText()+", ";  
	    } 
	 if(cb2.isChecked()) {  
		 	_int += cb2.getText()+", ";  
	    } 
	 if(cb3.isChecked()) {  
		 	_int += cb3.getText()+", ";  
	    } 
	 	 
	 if(et.getText().toString() == "")
	 	{
		 	_mes="no ";
	 	}
	else
	   	{
			_mes=et.getText().toString();
	    }

	   	_mes=_mes+" "+_sex;

	   	_mes=_mes+" "+_int;
	 	 
  }
  
  public void openDialog()
  {
	  AlertDialog.Builder dialog=new AlertDialog.Builder(GDD01.this);
	  dialog.setTitle("ABOUT");
	  dialog.setMessage(_mes);
	  dialog.setPositiveButton("確認", new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			finish();
			_mes="";
		}
	  });
	  dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			dialog.dismiss();
		}
	  });
	  dialog.show();
	  
  }
}

 

 

自我LV~