Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之Button事件實現與監聽方法總結

Android開發之Button事件實現與監聽方法總結

編輯:關於Android編程

本文實例總結了Android開發之Button事件實現與監聽方法。分享給大家供大家參考,具體如下:

先來介紹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;
   }
  }
 }
}

再來說說Button監聽方法

android button控件目前主要有如下幾種監聽方式:

一個button控件對應一個監聽:

Button buttontest;
buttontest = (Button) findViewById(R.id.button1);
buttontest.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  Log.i("TEST", "button onClick");
 }
});

多個button對應一個監聽1:

start = (Button) findViewById(R.id.button1);
stop = (Button) findViewById(R.id.button2);
start.setOnClickListener(mylistener );
stop.setOnClickListener(mylistener );
View.OnClickListener mylistener = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   switch (v.getId()) {
   case R.id.button1:
    Log.d(TAG, "Start to recorder video\n");
    start_recorde();
    break;
   case R.id.button2:
    Log.d(TAG, "Stop to recorder video\n");
    stop_recorde();
    break;
   default:
    break;
   }
  }
};

多個button對應一個監聽2:

public class MainActivity extends Activity implements View.OnClickListener{
 //界面元素
 private Button start;
 private Button stop;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  start = (Button) findViewById(R.id.button1);
  stop = (Button) findViewById(R.id.button2);
  start.setOnClickListener(this);
  stop.setOnClickListener(this);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.button1:
   Log.d(TAG, "Start to recorder video\n");
   break;
  case R.id.button2:
   Log.d(TAG, "Stop to recorder video\n");
   break;
  default:
   break;
  }
 }
}

xml中綁定監聽:

<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="mybuttonlistener">
</Button>

對應java代碼如下:

Button btn = (Button) findViewById(R.id.button1);
public void mybuttonlistener(View target){
 //do something5
}

從個人角度來講,不推薦使用第四種實現方式。

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved