安卓基本控件之Button

来源:互联网 发布:域名备案号 编辑:程序博客网 时间:2024/05/22 07:56

布局文件:

<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;
        }
    }
    
}

0 0
原创粉丝点击