Android开发入门 Button事件实现的方法(原创)
下面介绍Button事件实现的三种方法
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.64"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="248dp"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="248dp"
android:layout_height="wrap_content" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮" android:clickable="false"/>
</LinearLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWordActivity!</string>
<string name="app_name">HelloWord</string>
</resources>
第一种:
HelloWordActivity.java
package xyq.Android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
//import android.widget.TextView
public class HelloWordActivity extends Activity
{
/** Called when the activity is first created. */
private Button btn1;
@Override
public void onCreate(Bundle abc)
{
super.onCreate(abc);
/*TextView tv=new TextView(this);
tv.setText("你好,安卓!");
Button btn=new Button(this);
//btn.setWidth(150);
btn.setText("按钮1");
btn.setTextColor(Color.GREEN);
Button btn2=new Button(this);
btn2.setText("按钮2");
LinearLayout layout=new LinearLayout(this);
layout.addView(tv);
layout.addView(btn);
layout.addView(btn2);
this.setContentView(layout);*/
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.button1);
//使用匿名类注册Button事件
btn1.setOnClickListener(new OnClickListener ()
{
//这是错误的方法,on,不是On
public void OnClick(View v)
{
Toast.makeText(HelloWordActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(HelloWordActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
}
});
}
}
第二种:
HelloWordActivity.java
package xyq.Android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
//import android.widget.TextView
public class HelloWordActivity extends Activity
{
/** Called when the activity is first created. */
private Button btn1;
@Override
public void onCreate(Bundle abc)
{
super.onCreate(abc);
/*TextView tv=new TextView(this);
tv.setText("你好,安卓!");
Button btn=new Button(this);
//btn.setWidth(150);
btn.setText("按钮1");
btn.setTextColor(Color.GREEN);
Button btn2=new Button(this);
btn2.setText("按钮2");
LinearLayout layout=new LinearLayout(this);
layout.addView(tv);
layout.addView(btn);
layout.addView(btn2);
this.setContentView(layout);*/
setContentView(R.layout.main);
btn1.setOnClickListener(new ButtonClick());
}
//创建一个类,来响应OnClickListener
class ButtonClick implements OnClickListener
{
public void onClick(View v)
{
switch (v.getId()) {
case R.id.button1:
Toast.makeText(HelloWordActivity.this, "你点击了按钮1",Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
第三种:
Button控件有一个叫onClick的属性(在属性面板里叫“On Click”)。你可以通过这个属性设置要处理点击事件的方法名,然后在你的活动中实现这个方法。比如,你可以将你的Button控件属性设置为 onButtonClick。在XML中,这个属性将如下所示:
android:onClick="onButtonClick"
然后,在你的活动类,你需要实现这个方法。它应该是一个带有单个参数(一个View对象)的公有的void方法。例如,下面的按钮点击实现了当Button控件被点击时在屏幕生成一个消息框:
package xyq.Android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
//import android.widget.TextView
public class HelloWordActivity extends Activity
{
/** Called when the activity is first created. */
private Button btn1;
@Override
public void onCreate(Bundle abc)
{
super.onCreate(abc);
/*TextView tv=new TextView(this);
tv.setText("你好,安卓!");
Button btn=new Button(this);
//btn.setWidth(150);
btn.setText("按钮1");
btn.setTextColor(Color.GREEN);
Button btn2=new Button(this);
btn2.setText("按钮2");
LinearLayout layout=new LinearLayout(this);
layout.addView(tv);
layout.addView(btn);
layout.addView(btn2);
this.setContentView(layout);*/
setContentView(R.layout.main);
}
public void onButtonClick(View view)
{
Toast.makeText(this, "你点击了按钮1", Toast.LENGTH_SHORT).show();
}
}