3月9号Android开发学习

禁用与恢复按钮

在实际业务中按钮通常有两种状态,不可用状态和可用状态,它们的区别如下:

(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/but_enable"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="启用测试按钮"
            android:textColor="#000000"
            android:textSize="17dp"></Button>

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

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

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

</LinearLayout>
package com.example.myapplication;

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.myapplication.util.DateUtil;

public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

    private Button but_enable;
    private Button but_disable;
    private Button but_test;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_enable);
        but_enable = findViewById(R.id.but_enable);
        but_disable = findViewById(R.id.but_disable);
        but_test = findViewById(R.id.but_test);
        tv_result = findViewById(R.id.tv_result);

        but_enable.setOnClickListener(this);
        but_disable.setOnClickListener(this);
        but_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.but_enable:
                but_test.setEnabled(true);
                but_test.setTextColor(Color.BLACK);
                break;
            case R.id.but_disable:
                but_test.setEnabled(false);
                but_test.setTextColor(Color.GRAY);
                break;
            case R.id.but_test:
                String desc =String.format("%s", DateUtil.getNowTime());
                tv_result.setText(desc);
                break;
        }
    }
}

 

posted @ 2023-03-11 14:20  辞楠  阅读(23)  评论(0)    收藏  举报