package com.example.peterlee.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import static android.widget.CheckBox.*;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnClear, btnToast, btnAlertDialog, btnRadioButton;
private CheckBox checkbox, checkbox2;
private RadioButton radiobutton, radiobutton2;
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
btnClear = (Button)findViewById(R.id.btnClear);
btnClear.setOnClickListener(btnClear_Click);
btnToast = (Button)findViewById(R.id.btnToast);
btnToast.setOnClickListener(btnToast_Click);
btnAlertDialog = (Button)findViewById(R.id.btnAlertDialog);
btnAlertDialog.setOnClickListener(btnAlertDialog_Click);
checkbox =(CheckBox)findViewById(R.id.checkBox);
checkbox.setOnCheckedChangeListener(checkbox_click);
checkbox2=(CheckBox)findViewById(R.id.checkBox2);
checkbox2.setOnCheckedChangeListener(checkbox_click);
btnRadioButton=(Button)findViewById(R.id.btnRadioButton);
btnRadioButton.setOnClickListener(btnRadioButton_Click);
radiobutton=(RadioButton)findViewById(R.id.radioButton);
radiobutton2=(RadioButton)findViewById(R.id.radioButton2);
radiogroup=(RadioGroup)findViewById(R.id.radiogroup);
radiogroup.setOnCheckedChangeListener(radiogroup_click);
}
//btnClear Click 事件
private Button.OnClickListener btnClear_Click =
new Button.OnClickListener(){
public void onClick(View v) {
editText.setText("");
}
};
//btnToast Click 事件
private Button.OnClickListener btnToast_Click =
new Button.OnClickListener(){
public void onClick(View v) {
Toast toast=Toast.makeText(MainActivity.this, "Toast Message", Toast.LENGTH_LONG);
toast.show();
}
};
//btnAlertDialog Click 事件
private Button.OnClickListener btnAlertDialog_Click =
new Button.OnClickListener(){
public void onClick(View v) {
AlertDialog.Builder msg=new AlertDialog.Builder(MainActivity.this);
msg.setTitle("Title");
msg.setMessage("Close ?");
msg.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
finish();
}
});
msg.setNeutralButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
}
});
msg.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
}
});
msg.show();
}
};
//Checkbox Click事件
private OnCheckedChangeListener checkbox_click=new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String str;
str = "";
if (checkbox.isChecked())
{
str = str + "checkbox ";
};
if (checkbox2.isChecked())
{
str = str + "checkbox2 ";
};
editText.setText(str);
}
};
//btnRadioButton Click 事件, 動態產生RadioButton
private Button.OnClickListener btnRadioButton_Click =
new Button.OnClickListener(){
public void onClick(View v) {
RadioButton radiobutton;
radiobutton = new RadioButton(MainActivity.this);
radiobutton.setText("radiobutton");
radiogroup.addView(radiobutton);
}
};
//RadioGroup OnCheckedChangeListener事件
private RadioGroup.OnCheckedChangeListener radiogroup_click=
new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) {
int index=group.indexOfChild((RadioButton)findViewById(checkedId));
int count=group.getChildCount();
editText.setText("index=" + index);
//if (index==0) {
// editText.setText("index=1");
//}
}
};
}
沒有留言:
張貼留言