Android点击Button实现功能的几种方法总结
Android中Button控件應該算作是比較簡單的控件,然而,它的使用頻率卻是非常的高,今天,我在這里總結了三種常用的點擊Button實現(xiàn)其功能的方法。
1.很多時候,我們在用到Button控件時,往往都是“一次性”使用,這時,為了方便起見,我們一般采用的是匿名內(nèi)部類的方法,形如這樣:
button1.setOnClickListener(new OnClickListener() {
???@Override
???public void onClick(View v) {
????// TODO Auto-generated method stub
????System.out.println("您點擊了Button1");
???}
??});
我們可以看到,這樣的代碼不僅簡短,而且清晰易懂,不過,這樣的方法一般只是適用于這個Button使用的次數(shù)不多或是“一次性”使用
2.當Button有多個或者Button的使用次數(shù)很多時,我們需要采用綁定監(jiān)聽器的做法,其實,綁定監(jiān)聽器也有幾種方法,不過,我在這里就不一一列舉了,畢竟那些方法在實際的應用中也不常見。
我們一般的方法是實現(xiàn)OnClickListener接口,并實現(xiàn)其中的方法,正如這樣:
@Override
?public void onClick(View v) {
??// TODO Auto-generated method stub
??switch (v.getId()) {
??case R.id.button2:
???System.out.println("您點擊了Button2");
???break;
??default:
???break;
??}
?}
注:onClick方法是OnClickListen接口中的方法,我們實現(xiàn)這個接口就必須實現(xiàn)它的方法。
3.這是一種最為簡單的方法,我們需要做的就是添加一個方法并為Button添加一個屬性:
<Button?
??????? android:id="@+id/button3"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Button3 測試"
??????? android:onClick="clickHandler"
??????? />
其中,我們比平時多添加了onClick屬性。
那么,我們需要在代碼中添加我們在屬性中聲明的方法:
public void clickHandler(View view) {
??System.out.println("您點擊了Button3");
?}
最后,貼出完整的源代碼和實現(xiàn)效果截圖:
1.布局文件
<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"
??? tools:context=".MainActivity"?
??? android:orientation="vertical"
??? >
??? <TextView
??????? android:layout_width="wrap_content"
??????? android:layout_height="wrap_content"
??????? android:text="@string/hello_world" />
??? <Button?
??????? android:id="@+id/button1"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Button1 測試"
??????? />
???? <Button?
??????? android:id="@+id/button2"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Button2 測試"
??????? />
????? <Button?
??????? android:id="@+id/button3"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Button3 測試"
??????? android:onClick="clickHandler"
??????? />
</LinearLayout>
效果形如:
2.測試源代碼
復制代碼代碼如下:
package com.example.buttonclicktest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
?private Button button1 = null;
?private Button button2 = null;
?public void findButton() {
??button1 = (Button)findViewById(R.id.button1);
??button2 = (Button)findViewById(R.id.button2);
?}
?@Override
?protected void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.activity_main);
??findButton();
??button2.setOnClickListener(this);
??button1.setOnClickListener(new OnClickListener() {
???@Override
???public void onClick(View v) {
????// TODO Auto-generated method stub
????System.out.println("您點擊了Button1");
???}
??});
?}
?@Override
?public boolean onCreateOptionsMenu(Menu menu) {
??// Inflate the menu; this adds items to the action bar if it is present.
??getMenuInflater().inflate(R.menu.activity_main, menu);
??return true;
?}
?@Override
?public void onClick(View v) {
??// TODO Auto-generated method stub
??switch (v.getId()) {
??case R.id.button2:
???System.out.println("您點擊了Button2");
???break;
??default:
???break;
??}
?}
?public void clickHandler(View view) {
??System.out.println("您點擊了Button3");
?}
}
當我們點擊按鈕后,在Logcat中我們可以查看到結果如下所示:
從結果中我們可以看出,三種方法都可以實現(xiàn)按鈕點擊的功能,我們可以根據(jù)情況的不同選擇相應的方法。
總結
以上是生活随笔為你收集整理的Android点击Button实现功能的几种方法总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android UI开发第十四篇——可以
- 下一篇: android Log图文详解(Log.