- 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 HelloActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btnMethod01;
- Button btnMethod02;
- Button btnMethod03;
- btnMethod01 = (Button)findViewById(R.id.button1);
- btnMethod02 = (Button)findViewById(R.id.button2);
- btnMethod03 = (Button)findViewById(R.id.button3);
- //第一种方法:匿名类
- btnMethod01.setOnClickListener(new Button.OnClickListener(){
- public void onClick(View v){
- Toast.makeText(HelloActivity.this,R.string.method01
- ,Toast.LENGTH_SHORT).show();
- }
- });
- //添加监听事件
- btnMethod02.setOnClickListener(new button1OnClickListener());
- }
- //第二种方法:内部类实现 有两部 1.写内部类 2.添加监听事件
- private class button1OnClickListener implements OnClickListener{
- public void onClick(View v){
- Toast.makeText(HelloActivity.this,R.string.method02,
- Toast.LENGTH_SHORT).show();
- }
- }
- //第三种方法:用xml方法配置,该名称要与 main.xml 中button03的
- //android:onClick="OnClickButton03"的名字一样
- public void OnClickButton03(View v){
- Toast.makeText(HelloActivity.this,R.string.method03
- ,Toast.LENGTH_SHORT).show();
- }
- }
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 HelloActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnMethod01; Button btnMethod02; Button btnMethod03; btnMethod01 = (Button)findViewById(R.id.button1); btnMethod02 = (Button)findViewById(R.id.button2); btnMethod03 = (Button)findViewById(R.id.button3); //第一种方法:匿名类 btnMethod01.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ Toast.makeText(HelloActivity.this,R.string.method01 ,Toast.LENGTH_SHORT).show(); } }); //添加监听事件 btnMethod02.setOnClickListener(new button1OnClickListener()); } //第二种方法:内部类实现 有两部 1.写内部类 2.添加监听事件 private class button1OnClickListener implements OnClickListener{ public void onClick(View v){ Toast.makeText(HelloActivity.this,R.string.method02, Toast.LENGTH_SHORT).show(); } } //第三种方法:用xml方法配置,该名称要与 main.xml 中button03的 //android:onClick="OnClickButton03"的名字一样 public void OnClickButton03(View v){ Toast.makeText(HelloActivity.this,R.string.method03 ,Toast.LENGTH_SHORT).show(); } }
- <?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">
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method01"/>
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method02"/>
- <Button
- android:onClick="OnClickButton03"
- android:id="@+id/button3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/method03"/>
- </LinearLayout>
<?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"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/method01"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/method02"/> <Button android:onClick="OnClickButton03" android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/method03"/> </LinearLayout>