方法一:布局中只有一个按钮的时候,用匿名内部类,写法如下
package com.example.android_button;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Button btn_s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_s=(Button) this.findViewById(R.id.btn_s);
btn_s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点我一下", 1).show();
}
});
btn_s.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "长按一下", 1).show();
return false;
}
});
}
@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 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);
}
}
<RelativeLayout 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.android_button.MainActivity" >
<Button
android:id="@+id/btn_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="只有一个按钮"
/>
</RelativeLayout>
方法二:多个按钮的情况,为了增强代码可读性, OnClickListener接口写法如下
package com.example.android_button;
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 ButtonsActivity extends Activity implements OnClickListener{
private Button btn1,btn2,btn3;
/**
* 要从onCreate开始,不能直接就加载布局,下面的写法不对
*/
// @Override
// public void setContentView(int layoutResID) {
// // TODO Auto-generated method stub
// super.setContentView(R.layout.button);
//
// btn1=(Button) this.findViewById(R.id.button1);
// btn2=(Button) this.findViewById(R.id.button2);
// btn3=(Button) this.findViewById(R.id.button3);
//
// //实现监听方法
// btn1.setOnClickListener(this);
// btn2.setOnClickListener(this);
// btn3.setOnClickListener(this);
// }
/**
* 每一个activity(java类)对应一个窗口(xml文件设置窗体布局),都要从onCreate开始
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.button);
btn1=(Button) this.findViewById(R.id.button1);
btn2=(Button) this.findViewById(R.id.button2);
btn3=(Button) this.findViewById(R.id.button3);
//实现监听方法
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//得到按钮的id
switch(v.getId()){
case R.id.button1:
Toast.makeText(ButtonsActivity.this, "按钮一", 1).show();
break;
case R.id.button2:
Toast.makeText(ButtonsActivity.this, "按钮二", 1).show();
break;
case R.id.button3:
Toast.makeText(ButtonsActivity.this, "按钮三", 1).show();
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="46dp"
android:text="第一个按钮" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="19dp"
android:text="第二个按钮" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="32dp"
android:text="第三个按钮" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_button"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<!--
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
-->
</activity>
<!-- 把 ButtonsActivity设置为启动项-->
<activity
android:name=".ButtonsActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
方法三:响应XML定义的事件
XML中代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 单击的方法名称onClick="sendMessage" -->
<Button
android:id="@+id/btnxml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="216dp"
android:text="Button"
android:onClick="sendMessage"
/>
</RelativeLayout>
对应的activity中的代码
package com.example.android_button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ButtonXML extends Activity{
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonxml);
button1=(Button) this.findViewById(R.id.btnxml);
}
//响应按钮的点击事件
public void sendMessage(View view){
Toast.makeText(ButtonXML.this, "响应XML定义的事件", 1).show();
}
}
运行效果图如下