技海泛舟(个人技术研究)

  • 首页
  • 日积月累
  • 学习计划
  • 随想
  • project
  • 关于
技海泛舟
一个技术宅的博客
  1. 首页
  2. 日积月累
  3. Android
  4. 正文

Android-Button(按钮控件)

2022年10月3日 1138点热度

按钮控件Button由TextView派生而来,但又有区别:

  • Button拥有默认的按钮背景,TextView默认无背景;
  • Button的内部文本默认居中对齐,TextView默认左对齐;
  • Button默认将英文字母转为大写,TextView默认保持原始的英文大小写。
    根据是否要使用带有文本,图标或两者的按钮,可以通过三种方式在布局中创建按钮:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonActivity">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="默认文本"
        android:textColor="@color/black"
        android:textSize="20sp"
        tools:ignore="MissingConstraints"></TextView>
    
    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Hello(Onclick方式)"
        app:layout_constraintTop_toBottomOf="@+id/tv"
        android:onClick="doClick"
        tools:ignore="MissingConstraints" />
    <Button
        android:id="@+id/btn02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Hello World!"
        android:textAllCaps="false"
        app:layout_constraintTop_toBottomOf="@+id/btn01"
        tools:ignore="MissingConstraints"></Button>
    <Button
        android:id="@+id/btn03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="图标按钮"
        android:drawableLeft="@mipmap/ic_launcher"
        app:layout_constraintTop_toBottomOf="@+id/btn02"        tools:ignore="MissingConstraints"></Button>
    </androidx.constraintlayout.widget.ConstraintLayout>

    button的常见属性及属性值

常用属性 属性值
android:id(唯一识别) @android:xxx/@id/xxx
android:text(按钮框内容) 自行定义
android:layout_width(宽度) match_parent(“填充满”父容器)/wrap_parent(据内容大小进行填充)/自行定义,如10dp\20sp
android:layout_height(高度) 同上
android:background (背景) #000000黑色/@android:color/background_light系统给出的样式、颜色或自行定义的样式颜色
android:textColor(文本颜色) 同上,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
android:textSize(文本大小) 如:16dp
android:paddingTop/paddingBottom(该控件内部内容距离该控件上/下边缘的边距) 如:5dp
android:paddingRight/paddingLeft(该控件内部内容距离该控件右/左边缘的边距) 如:10dp
android:layout_marginTop/layout_marginBottom(该属性所在控件的边缘与上/下部控件的边缘的距离) 如:10dp
android:layout_marginRight/layout_marginLeft (该属性所在控件的边缘与右/左部控件的边缘的距离) 如:10dp
android:gravity(自身内部元素的对齐方式) center/center_vertical/center_horizontal/fill/left/right/bottom/top…
android:drawable (放一个drawable资源)
android:drawableTop(可拉伸要绘制的文本的上面)
android:drawableBottom (可拉伸要绘制的文本的下面)
android:drawableLeft (可拉伸要绘制的文本的左侧)
android:drawableRight (可拉伸要绘制的文本的右侧)
android:onClick(设置点击事件) 如:doClickFn

button常用状态

常用状态 状态值
android:clickable(是否允许点击) true/false
android:textAllCaps(是否允许大小写) true/false
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中状态
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用

按钮控件的常用的监听器方法:

1.点击监听器。setOnClickListener方法设置。按钮被按住少于500ms时,会触发点击事件。

        tv_result = findViewById(R.id.tv);
        //方式一、监听方式
        btn02 = findViewById(R.id.btn02);
        btn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String desc = String.format("%s 点击了按钮:%s", Utils.getNowTime(),((Button)view).getText());
                tv_result.setText(desc);
                Toast.makeText(ButtonActivity.this, "Clicked点击了btn02", Toast.LENGTH_SHORT).show();
            }
        });

2.长按监听器:通过setOnLongClickListener方法设置。按钮被按住超过500毫秒,会触发长按事件

        tv_result = findViewById(R.id.tv);
        Button btn03 = findViewById(R.id.btn03);
        btn03.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                String desc = String.format("%s 点击了按钮:%s", Utils.getNowTime(),((Button)view).getText());
                tv_result.setText(desc);
                Toast.makeText(ButtonActivity.this, "Clicked点击了btn03", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

3.通过onClick属性或重新onClick方法实现

package com.example.myapplication_test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication_test.utils.Utils;

public class ButtonActivity extends AppCompatActivity {

    private TextView tv_result;

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

        tv_result = findViewById(R.id.tv);
    }
    // 在xml中设置 android:onClick="doClick"
    public void doClick(View view) {
//        view类型转换成button类型,并获取文本
        String desc = String.format("%s 点击了按钮:%s", Utils.getNowTime(),((Button)view).getText());
        tv_result.setText(desc);
        Toast.makeText(ButtonActivity.this, "Clicked点击了", Toast.LENGTH_SHORT).show();
    }
}
  • 重写onClick()方法,或自定义onClick,现在已不推荐
    public class MainActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        Button btn = (Button) findViewById(R.id.btn);
        //绑定监听
        btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
    }
    }

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2022年10月4日
< 上一篇
下一篇 >
归档
  • 2024 年 11 月
  • 2024 年 8 月
  • 2024 年 5 月
  • 2024 年 2 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 9 月
  • 2023 年 6 月
  • 2022 年 12 月
  • 2022 年 11 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 8 月
  • 2022 年 7 月
  • 2022 年 6 月
  • 2022 年 5 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2022 年 1 月
  • 2021 年 12 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 5 月
分类
  • Android
  • Arduino
  • cordova
  • css
  • go
  • html5
  • JavaScript
  • nodejs
  • oracle
  • project
  • system
  • uni-app
  • vscode
  • vue
  • 学习计划
  • 摘抄
  • 随想
最新 热点 随机
最新 热点 随机
windows安装mysql ,VSCODE连接MySQL数据库 创建api的逻辑 观看七战奥运会 德国波尔告别 go utils工具 Go语言中的sort包帮我们实现了对任一类型的数组进行排序。 vue + go安装
Go language single file creation user Android-编辑框EditText js动态添加和移除disabled属性和style css3制作圣诞树 Google的十诫

COPYRIGHT © 技海泛舟(个人技术研究). 2021-2023. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

黑ICP备19002110号-1

黑公网安备 23060202000432号