android学习之旅(7)---button

Button

Button继承了TextView组件,它主要是在UI界面上生成一个按钮,该按钮可以供用户点击,当用户点击Button时,会自动触发onClick事件。下面是Button的一些常见的用法:

(1)普通按钮
(2)圆角按钮
(3)描边按钮
(4)按压变色按钮
(5)按钮点击事件

普通按钮

由于Button继承于TextView所以,TextView的属性也适用于Button。

示例程序1

    <!--布局使用相对布局-->
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF2600"
        android:text="ButtonOne"
        android:textColor="#00ff00"
        android:textSize="30sp"/>

圆角按钮

如果需要给按钮添加样式,主要是通过修改android:background属性来实现。

  • (1)在res/drawable下添加XML文件。eg:botton_two_style.xml
  • (2)选择根元素为:shape
  • (3)在XML中选择感兴趣的属性进行修改。
  • (4)通过android:background来使用该xml文件

botton_two_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置圆角的半径为5dp-->
    <corners android:radius="5dp"></corners>
    <!--设置组件的填充颜色-->
    <solid android:color="#ff00ff"></solid>
</shape>

示例程序2

    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_one"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_two_style"
        android:text="ButtonTwo"
        android:textColor="#000000"
        android:textSize="30sp"/>

描边按钮

shape中有6个基本子元素:corners(圆角)、gradient(渐变)、padding(内边距)、size(尺寸)、solid(填充)、stroke(描边)。刚才的示例程序中展示corners的功能,这个列子将展示描边的功能,步骤和刚才是一样的。

button_three_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <stroke android:width="1dp"
            android:color="#00FFD9"/>
</shape>

示例程序3

    <Button
        android:id="@+id/btn_Three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_three_style"
        android:layout_below="@id/btn_two"
        android:onClick="showToast"
        android:textSize="30sp"
        android:textColor="#2196F3"
        android:text="ButtonThree"/>

这里推荐一篇文章:简书:Android shape属性大全

按压变色按钮

按压变色这个功能在我们平时的使用会经常使用到,那它是如何实现的?利用selector选择器我们可以是实现这个功能。

button_four_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--没有被按压的效果-->
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#0099FF"/>
        </shape>
    </item>
    <!--按压效果-->
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#3F51B5"/>
        </shape>
    </item>
</selector>

示例程序4

    <Button
        android:id="@+id/btn_four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_four_style"
        android:layout_below="@id/btn_Three"
        android:text="ButtonFour"
        android:textSize="30sp"/>

按钮点击事件

给按键添加点击事件两种方法:

(1)onClick

通过onClick属性可以为按钮添加一个点击事件。

  • (1)在onclick中指定点击事件触发方法的方法名。
  • (2)在对应的.java文件中实现该方法。

需要注意的是:方法的形式必须为:public void 方法名(View),方法有一个类型为View的参数。示例程序参照:示例程序3

(2) 在.java中为对应按钮设置对应的点击事件
  • (1)通过findViewById方法找到对应的按钮
  • (2)通过setOnClickListener方法设置点击事件
  • (3)重写View.OnClickListeneronClick方法

示例程序参照:示例程序4

.java

    private Button m_ButtonFour;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_test);
        m_ButtonFour=findViewById(R.id.btn_four);
        m_ButtonFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取按钮文本内容
                String str=((Button) v).getText().toString();
                str+="被点击";
                Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void showToast(View view)
    {
        String str=((Button) view).getText().toString();
        str+="被点击";
        Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
    }

运行效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KKLEkkox-1575622216314)(./button运行效果.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.example.animdialogdemo; import com.example.calculatedemo.R; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class CalculatorDialog extends Dialog implements OnClickListener { private Button[] btn = new Button[10]; private EditText etLed; private Button btnSub, btnPlus, btnEqual, btnDot, btnC, mButton_cancel, btnOk; private double predata = 0; private String preopt = "="; private boolean vbegin = true; private int i = 1; public CalculatorDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_caclu); findUiById(); addListener(); } private void findUiById() { etLed = (EditText) this.findViewById(R.id.ed_led); btn[0] = (Button) this.findViewById(R.id.mButton0); btn[1] = (Button) this.findViewById(R.id.mButton1); btn[2] = (Button) this.findViewById(R.id.mButton2); btn[3] = (Button) this.findViewById(R.id.mButton3); btn[4] = (Button) this.findViewById(R.id.mButton4); btn[5] = (Button) this.findViewById(R.id.mButton5); btn[6] = (Button) this.findViewById(R.id.mButton6); btn[7] = (Button) this.findViewById(R.id.mButton7); btn[8] = (Button) this.findViewById(R.id.mButton8); btn[9] = (Button) this.findViewById(R.id.mButton9); btnSub = (Button) this.findViewById(R.id.mButton_jia); btnPlus = (Button) this.findViewById(R.id.mButton_jian); btnEqual = (Button) this.findViewById(R.id.mButton_equ); btnDot = (Button) this.findViewById(R.id.mButton_dian); btnC = (Button) findViewById(R.id.mButton_C); btnOk = (Button) this.findViewById(R.id.mButton_ok); mButton_cancel = (Button) this.findViewById(R.id.mButton_cancel); } public void addListener() { for (int i = 0; i < btn.length; i++) { btn[i].setOnClickListener(this); } btnOk.setOnClickListener(this); btnSub.setOnClickListener(this); btnPlus.setOnClickListener(this); btnEqual.setOnClickListener(this); btnDot.setOnClickListener(this); btnC.setOnClickListener(this); mButton_cancel.setOnClickListener(this); } public void onClick(View v) { String command = ((Button) v).getText().toString(); String str = etLed.getText().toString(); if (command.compareTo("bac") == 0) { if (str.length() > 1) { etLed.setText(str.substring(0, str.length() - 1)); } else if (str.length() == 1) { etLed.setText("0"); vbegin = true; } } else if (command.compareTo("C") == 0) { etLed.setText("0"); vbegin = true; predata = 0; preopt = "="; } else if ("0123456789".indexOf(command) != -1) { wtNumber(command); } else if ("+-=".indexOf(command) != -1) { wtOperater(command); } if ("ȷ��".equals(command)) { this.dismiss(); } switch (v.getId()) { case R.id.mButton_cancel: dismiss(); break; default: break; } } private void wtNumber(String str) { if (vbegin) { etLed.setText(str); } else { etLed.append(str); } vbegin = false; } private void wtOperater(String opt) { try { double temp = Double.parseDouble(etLed.getText().toString()); if (vbegin) { preopt = opt; } else { if (preopt.equals("=")) { predata = temp; } else if (preopt.equals("+")) { predata += temp; } } etLed.setText(predata + ""); preopt = opt; } catch (NumberFormatException e) { etLed.setText("����Ϊ0"); } catch (ArithmeticException e) { etLed.setText("����Ϊ0"); preopt = "="; } finally { vbegin = true; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值