安卓基本控件之Button

佈局文件:

<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:orientation="vertical" >

    <!--系統默認的
      android:background="@null"  去掉背景顏色
     android:background="@android:color/transparent" 背景顏色設置爲透明
     
      android:drawableTop="@drawable/ic_launcher"  按鈕內容頂部加圖片
        android:drawableBottom="@drawable/ic_launcher" 按鈕內容底部加圖片
        android:drawableLeft="@drawable/ic_launcher" 按鈕內容左側加圖片
        android:drawableRight="@drawable/ic_launcher" 按鈕內容右側加圖片
     -->
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕1"
        />
    <Button
         android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕2"
        android:background="@null"
        />
    <Button
         android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕3"
        android:background="@android:color/transparent"
        />
    <Button
         android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按鈕4"
        android:drawableTop="@drawable/ic_launcher"
        android:drawableBottom="@drawable/ic_launcher"
        android:drawableLeft="@drawable/ic_launcher"
        android:drawableRight="@drawable/ic_launcher"
        android:onClick="MyonClickBtn"
        />
</LinearLayout>

Java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
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 static final String TAG = "MainActivity";
    //聲明按鈕控件
    private Button btn1,btn2,btn3;
    private Button btn4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //將佈局加載進來
        setContentView(R.layout.activity_main);
        //控件通過  findViewById 找到相應的控件
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        //設置按鈕的監聽事件
        //第一種監聽方法    匿名內部類
        
        //第一種導包方式 View.OnClickListener()
        //第二種 導包   import android.view.View.OnClickListener;
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("點擊了按鈕1。。。");
                //Log.i("===AAA===", "點擊了按鈕1!!!");//打印log日誌
                Log.i(TAG, "點擊了按鈕1!!!");//打印log日誌
                Toast.makeText(MainActivity.this, "點擊了按鈕1!!!", Toast.LENGTH_SHORT).show();
                //Toast.makeText(MainActivity.this, "點擊了按鈕1!!!", 0).show();
            }
        });
        //第二種方法  類實現監聽的接口  適合按鈕多的時候使用
        btn2.setOnClickListener(this);
        //第三種方法   成員內部類  
        MyClick myClick = new MyClick();
        btn3.setOnClickListener(myClick);
    }


    @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 v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn2:
            Toast.makeText(MainActivity.this, "點擊了按鈕2!!!", Toast.LENGTH_SHORT).show();
            break;
//        case R.id.btn3:
//            Toast.makeText(MainActivity.this, "點擊了按鈕3!!!", Toast.LENGTH_SHORT).show();
//            break;

        default:
            break;
        }
    }
    
    //第三種方法
    public class MyClick implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.btn3:
                Toast.makeText(MainActivity.this, "點擊了按鈕3!!!", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
            }
        }
        
    }
    
    //第四種方法  
    //xml 佈局文件  實現 監聽事件
    //方法名必須和xml的方法名相同   而且  必須有參數 View  找到點擊了那個按鈕
    public void MyonClickBtn(View v){
        switch (v.getId()) {
        case R.id.btn4:
            Toast.makeText(MainActivity.this, "點擊了按鈕4!!!", Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章