下面介绍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;
- }
- }
- }
- }
第三种:
当程序里面有很多的部件需要点击执行动作的时候可以使用implements OnClickListener
看下面的代码:
- public class Dong_Home extends TabActivity implements OnClickListener{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_dish);
- findViewById(R.id.tab_imageView_coldDish).setOnClickListener(this);
- findViewById(R.id.tab_imageView_hotDish).setOnClickListener(this);
- findViewById(R.id.tab_imageView_seafood).setOnClickListener(this);
- findViewById(R.id.tab_imageView_drink).setOnClickListener(this);
- findViewById(R.id.tab_imageView_soup).setOnClickListener(this);
- findViewById(R.id.tab_imageView_food).setOnClickListener(this);
- findViewById(R.id.tab_imageView_fanhui).setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.tab_imageView_coldDish:
- break;
- case R.id.tab_imageView_hotDish:
- break;
- case R.id.tab_imageView_seafood:
- break;
- case R.id.tab_imageView_drink:
- break;
- case R.id.tab_imageView_soup:
- break;
- case R.id.tab_imageView_food:
- break;
- case R.id.tab_imageView_fanhui:
- finish();
- break;
- }
- }
- }
可以直接在 active_main.xml 文件中直接定义事件
<Button
android:id="@+id/btnclick"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btnclick_click"
/>
在.java 的页面直接写方法名即可
public void btnclick_click(View v)
{
Button tbtn = (Button)findViewById(R.id.btnclick);
tbtn.setText("my click");
}