Android开发之Button事件实现方法的总结
下面介绍Button事件实现的两种方法
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/myButton1"
- android:text=" 按钮1 "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/myButton2"
- android:text=" 按钮2 "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, ButtonDemoActivity!</string>
- <string name="app_name">ButtonDemo</string>
- </resources>
第一种:
ButtonDemoActivity.java
- package com.android.ButtonDemo.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class ButtonDemoActivity extends Activity {
- Button myButton1,myButton2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myButton1=(Button)findViewById(R.id.myButton1);
- myButton2=(Button)findViewById(R.id.myButton2);
- //使用匿名类注册Button事件
- myButton1.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
- }
- });
- myButton2.setOnClickListener(new OnClickListener()
- {
- public void onClick(View v)
- {
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
- }
- });
- }
- }
第二种:
ButtonDemoActivity.java
- package com.android.ButtonDemo.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class ButtonDemoActivity extends Activity {
- Button myButton1,myButton2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myButton1=(Button)findViewById(R.id.myButton1);
- myButton2=(Button)findViewById(R.id.myButton2);
- myButton1.setOnClickListener(new ButtonClick());
- myButton2.setOnClickListener(new ButtonClick());
- }
- //创建一个类,来响应OnClickListener
- class ButtonClick implements OnClickListener
- {
- public void onClick(View v)
- {
- switch (v.getId()) {
- case R.id.myButton1:
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
- break;
- case R.id.myButton2:
- Toast.makeText(ButtonDemoActivity.this, "你点击了按钮2",Toast.LENGTH_LONG).show();
- break;
- default:
- break;
- }
- }
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。