主要通过三个按键来演示,每个按键用一种实现方法,效果通过Toast观察。
第一种:在布局文件(XML)直接在button控件下添加属性onclick,属性的内容即为按键的响应方法。(方法权限必需为public)
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn_1"
android:onClick="btn_Onclick"/> public void btn_Onclick(View view){
Toast.makeText(getBaseContext(), "btn_1 have been clicked!", Toast.LENGTH_SHORT).show();
}第二种:在activity内绑定按键,设置监听器并实现。
btn_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "btn_2 have been clicked", Toast.LENGTH_SHORT).show();
}
});第三种:在activity实现onclicklistener接口,并在onclick方法实现。
public class MainActivity extends Activity implements OnClickListener
public void onClick(View view) {
// TODO Auto-generated method stub
// 当只有一个Button的时候可以这样写
// Toast.makeText(getBaseContext(), "btn_3 have benn clicked", Toast.LENGTH_SHORT).show();
// 当存在多个Button的时候
switch (view.getId()) {
case R.id.btn_3://控件的ID号
Toast.makeText(getBaseContext(), "btn_3 have benn clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.androidbuttondemo.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn_1"
android:onClick="btn_Onclick"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn_2"/>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="btn_3"/>
</LinearLayout>
package com.example.androidbuttondemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_1,btn_2,btn_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1 = (Button)findViewById(R.id.btn_1);
btn_2 = (Button)findViewById(R.id.btn_2);
btn_3 = (Button)findViewById(R.id.btn_3);
// 第二种:内部定义监听器并实现
btn_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "btn_2 have been clicked", Toast.LENGTH_SHORT).show();
}
});
// 第三种:实现onclicklistener接口
btn_3.setOnClickListener(this);
}
// 第一种:通过XML控件的属性定义按键的点击 onclick
public void btn_Onclick(View view){
Toast.makeText(getBaseContext(), "btn_1 have been clicked!", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
// 当只有一个Button的时候可以这样写
// Toast.makeText(getBaseContext(), "btn_3 have benn clicked", Toast.LENGTH_SHORT).show();
// 当存在多个Button的时候
switch (view.getId()) {
case R.id.btn_3://控件的ID号
Toast.makeText(getBaseContext(), "btn_3 have benn clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
效果:
本文介绍了Android中按键的三种响应方法,通过实例演示了如何在布局XML中设置onclick属性、使用OnClickListener以及Override dispatchTouchEvent方法来处理按键事件。并以Toast显示结果,详细解释了每种方法的适用场景。

1230

被折叠的 条评论
为什么被折叠?



