Android--控件Button的详细用法介绍(适合初学者)

Button是Android中一个非常简单的控件,在我们平时的项目中,可以说是非常的常见,使用率也是相当高。本身对于这个控件的用法也是很好掌握的,下面我们就从几个方面介绍一下它的用法,以及一些自定义Button的使用。

首先讲讲Button的一些基本用法:


<Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="按钮1"
        />

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:background="#ff0000"
        />
////这里我们只是使用"android:background"为按钮添加了一张图片背景(我事先准备好的,放置在mipmap下)
  

使用点9图片:
一般而言,我们安卓android studio的时候有一个SDK,而点9工具默认的路径为“SDK–tools–draw9patch.bat”这一个就是我们的点9工具。当然了,我们也可以直接配置到android studio中,这样我们可以直接在as中打开。在刚刚那个路径下直接打开也是可以的。再或者我们在该目录下找不到draw9patch.bat,那么我们不妨就直接新建一张图片,重新命名为.9.XX格式,直接拖拽至我们的项目,也可以触发这个工具。那么点9工具怎么使用呢?

点9图片处理好之后直接引用就可以啦!

  <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点9图片按钮"
        android:background="@mipmap/blue_deal"
        />

使用shape来自定义按钮形状:
drawable下新建btn_shape.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@color/grey"/>
shape>
<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="圆角图片"
        android:background="@drawable/btn_shape"
        />

使用style下的自定义样式:

 
    <style name="btnStyle">
        <item name="android:background">@drawable/btn_shape
        "android:textColor">#ffffff
        "android:textSize">22sp
    style>
 <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="通用样式"
        style="@style/btnStyle"
        />

通过selector来使用按钮的选择变化:
drawable下新建btn_selector.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_pressed="true" >
        <shape>
            <corners android:radius="10dp">corners>
            <solid android:color="@color/red">solid>
            <stroke android:width="2dp" android:color="#FFC626">stroke>
        shape>
    item>
    
    <item>
        <shape>
            <corners android:radius="10dp">corners>
            <solid android:color="@color/black">solid>
            <stroke android:width="2dp" android:color="#FFC626">stroke>
        shape>
    item>
selector>
<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="改变按钮背景"
        android:textColor="#ffffff"
        android:background="@drawable/btn_selector"
        />

下面我们讲解一些按钮的监听事件(常用的两种):

一:通过实现OnClickListener接口

public class MainActivity extends Activity implements View.OnClickListener {
//实现OnClickListener接口
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        //找到Button,因为是返回的是VIEW,所以我们进行强转
        Button btn = (Button) findViewById(R.id.btn);
        //绑定监听
        btn.setOnClickListener(this);
    }
//重写onClick()方法
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
    }
}

二:使用匿名内部类

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);

        Button btn = (Button) findViewById(R.id.btn);
        //使用匿名内部类
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

具体使用哪种看个人喜好咯!

你可能感兴趣的:(Android)