读书人

button3-兑现OnClickListener接口

发布时间: 2012-09-20 09:36:50 作者: rapoo

button3---实现OnClickListener接口

?

public class Test extends Activity implements OnClickListener {

? ? @Override

? ? public void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.main);

? ? ? ? init();

? ? }

?

? ? private void init() {

? ? ? ? // TODO Auto-generated method stub

? ? ? ? Button button1 = (Button) findViewById(R.id.button1);

? ? ? ? Button button2 = (Button) findViewById(R.id.button2);

? ? ? ? Button button3 = (Button) findViewById(R.id.button3);

? ? ? ? button1.setOnClickListener(this);

? ? ? ? button2.setOnClickListener(this);

? ? ? ? button3.setOnClickListener(this);

? ? }

?

? ? @Override

? ? public void onClick(View v) {

? ? ? ? switch (v.getId()) {

? ? ? ? ? ? case R.id.button1:

? ? ? ? ? ? ? ? showDialog(1);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case R.id.button2:

? ? ? ? ? ? ? ? showDialog(2);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case R.id.button3:

? ? ? ? ? ? ? ? showDialog(3);

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? default:

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? }

?

? ? @Override

? ? protected Dialog onCreateDialog(int id) {

? ? ? ? switch (id) {

? ? ? ? ? ? case 1: {

?

? ? ? ? ? ? ? ? Dialog dialog = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setMessage("one111111111111111111111")// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog;

? ? ? ? ? ? }

? ? ? ? ? ? case 2: {

? ? ? ? ? ? ? ? ScrollView sv = new ScrollView(this);

? ? ? ? ? ? ? ? TextView tv = new TextView(this);

? ? ? ? ? ? ? ? tv.setText("2");

? ? ? ? ? ? ? ? sv.addView(tv);

? ? ? ? ? ? ? ? Dialog dialog2 = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setView(sv)// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog2;

? ? ? ? ? ? }

? ? ? ? ? ? case 3: {

? ? ? ? ? ? ? ? ScrollView sv = new ScrollView(this);

? ? ? ? ? ? ? ? TextView tv = new TextView(this);

? ? ? ? ? ? ? ? tv.setText("3");

? ? ? ? ? ? ? ? sv.addView(tv);

? ? ? ? ? ? ? ? Dialog dialog3 = new AlertDialog.Builder(Test.this).setTitle("Question")

?

? ? ? ? ? ? ? ? .setView(sv)// 设置内容

? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("Yes", // 设置确定按钮

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? setResult(RESULT_OK);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }).setNegativeButton("No", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int whichButton) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }).create();// 创建

? ? ? ? ? ? ? ? return dialog3;

? ? ? ? ? ? }

? ? ? ? ? ? default: {

? ? ? ? ? ? ? ? return null;

? ? ? ? ? ? }

? ? ? ? }

? ? }

?

}


读书人网 >移动开发

热点推荐