AndroidStudio-按钮触控

一、按钮控件Button

1.按钮控件Button由TextView派生而来,它们之间的区别有:

(1)Button拥有默认的按钮背景,而TextView默认无背景;

(2)Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;

(3)Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文默认大写"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textSize="17sp"/>
    

</LinearLayout>

这里TextView默认靠左对齐,加上gravity后可居中

2.按钮控件的新增属性

与TextView相比,Button增加了两个新属性:

(1)textAllCaps属性,它指定了是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文默认大写"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文保持原状"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"/>


</LinearLayout>

(2)onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法;

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文默认大写"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文保持原状"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="直接指定点击方法"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:onClick="doClick"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文默认大写"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下面的按钮英文保持原状"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="直接指定点击方法"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:onClick="doClick"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里查看按钮的点击结果"
        android:textColor="@color/black"
        android:textSize="17sp"/>

</LinearLayout>

先创建一个工具包获取当前时间:

package com.example.chapter01.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:MM:SS");
        return sdf.format(new Date());
    }
}

然后Java代码:

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.chapter01.utils.DateUtil;

public class ButtonStyleActivity extends AppCompatActivity {

    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_style);
        tv_result = findViewById(R.id.tv_result);
    }

    public void doClick(View view) {
        String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button) view).getText());
        tv_result.setText(desc);
    }
}

发布运行:

然后点击按钮:

二、点击事件和长按事件

监听器,意思是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。

按钮控件有两种常用的监听器:

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

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_click_singe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定单独的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="15sp"
        android:text="这里查看按钮的点击结果"/>

</LinearLayout>

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.chapter01.utils.DateUtil;

public class ButtonClickActivity extends AppCompatActivity {

    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_click);
        tv_result = findViewById(R.id.tv_result);
        Button btn_click_single = findViewById(R.id.btn_click_singe);
        btn_click_single.setOnClickListener(new MyOnclickListener(tv_result));
    }

    static class MyOnclickListener implements View.OnClickListener{
        private final TextView tv_result;
        public MyOnclickListener(TextView tv_result) {
            this.tv_result = tv_result;
        }

        @Override
        public void onClick(View v) {
            String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button) v).getText());
            tv_result.setText(desc);
        }
    }
}

发布运行:

点击按钮:

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

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_long_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定长按的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="15sp"
        android:text="这里查看按钮的点击结果"/>

</LinearLayout>

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter01.utils.DateUtil;

public class ButtonLongClickActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_long_click);
        TextView tv_result = findViewById(R.id.tv_result);
        Button btn_long_click = findViewById(R.id.btn_long_click);
        btn_long_click.setOnLongClickListener(v -> {
            String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button) v).getText());
            tv_result.setText(desc);
            return true;
        });
    }
}

长按按钮:

三、禁用与恢复按钮

在实际业务中,按钮通常拥有两种状态,即不可用状态与可用状态,它们在外观和功能上
的区别如下:

1.不可用按钮:按钮不允许点击,即使点击也没反应,同时按钮文字为灰色;

2.可用按钮:按钮允许点击,点击按钮会触发点击事件,同时按钮文字为正常的黑色;

是否允许点击由enabled属性控制,属性值为true时表示允许点击,为false时表示不允许点击。

例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_enable"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="启用测试按钮"
            android:textColor="#000000"
            android:textSize="17sp"/>

        <Button
            android:id="@+id/btn_disable"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="禁用测试按钮"
            android:textColor="#000000"
            android:textSize="17sp"/>
    </LinearLayout>


        <Button
            android:id="@+id/btn_test"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="测试按钮"
            android:textColor="#000000"
            android:textSize="17sp"/>

        <TextView
            android:id="@+id/tv_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="这里查看测试按钮的点击结果"
            android:textColor="#000000"
            android:textSize="17sp"/>

</LinearLayout>

package com.example.chapter01;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter01.utils.DateUtil;

public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_test;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_enable);
        Button btn_enable = findViewById(R.id.btn_enable);
        Button btn_disable = findViewById(R.id.btn_disable);
        btn_test = findViewById(R.id.btn_test);
        tv_result = findViewById(R.id.tv_result);

        btn_enable.setOnClickListener(this);
        btn_disable.setOnClickListener(this);
        btn_test.setOnClickListener(this);

    }

    @Override                 
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_enable:
                //启用当前控件
                btn_test.setEnabled(true);
                //设置按钮的文字颜色
                btn_test.setTextColor(Color.BLACK);
                break;
            case R.id.btn_disable:
                //禁用当前控件
                btn_test.setEnabled(false);
                btn_test.setTextColor(Color.GRAY);
                break;
            case R.id.btn_test:
                String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button) v).getText());
                tv_result.setText(desc);
                break;
        }
    }
}

点击按钮进行测试:

### 实现按钮居中布局的方法 在 Android Studio 中,要使按钮居中显示,可以通过多种布局方式实现。以下是几种常见的方法: #### 使用 `RelativeLayout` 进行居中 当使用相对布局 (`RelativeLayout`) 时,可以使按钮相对于父容器居中。具体做法是在 XML 文件中定义按钮并设置其属性。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/centered_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Center Me!" android:layout_centerInParent="true"/> </RelativeLayout> ``` 此代码片段展示了如何利用 `RelativeLayout` 的特性让按钮在其内部水平和垂直方向上都处于中央位置[^1]。 #### 利用 `ConstraintLayout` 完成居中操作 对于更复杂的界面设计,推荐采用约束布局 (`ConstraintLayout`) 来达到同样的效果。这种方式不仅能够更好地管理视图之间的关系,而且性能优越于传统的绝对定位或相对定位方案。 ```xml <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/constraint_layout_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Constrain Center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 上述例子说明了怎样借助 `ConstraintLayout` 将按钮放置在整个屏幕上最中间的位置[^2]。 #### 应用 `LinearLayout` 并配合重力参数调整 如果偏好简单结构,则可以选择线性布局 (`LinearLayout`) 结合特定的重力(`gravity`) 和填充(`padding/margin`) 参数来达成目标。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/linear_layout_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Linear Layout Button"/> </LinearLayout> ``` 这里通过设定 LinearLayout 的 gravity 属性为 "center", 让其中包含的所有组件自动向中心靠拢[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值