app简单控件了解——按钮——禁用与恢复按钮

 

 

 

 

 

 

 

 

 

 

 

<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_height="wrap_content"
            android:layout_weight="1"
            android:text="启用测试按钮"
            android:textColor="#000000"
            android:textSize="17sp" />

        <Button
            android:id="@+id/btn_disable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            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:enabled="false"
        android:text="测试按钮"
        android:textColor="#888888"
        android:textSize="17sp" />

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

</LinearLayout>

 

 

 

package com.example.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    private TextView tv_result; // 声明一个文本视图实例
    private Button btn_test; // 声明一个按钮控件实例

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

        tv_result = findViewById(R.id.tv_result); // 获取名叫tv_result的文本视图

        // 因为按钮控件的setOnClickListener方法来源于View基类,所以也可对findViewById得到的视图直接设置点击监听器

        findViewById(R.id.btn_enable).setOnClickListener(this);
        findViewById(R.id.btn_disable).setOnClickListener(this);

        btn_test = findViewById(R.id.btn_test); // 获取名叫btn_test的按钮控件
        btn_test.setOnClickListener(this); // 设置btn_test的点击监听器
    }

    @Override
    public void onClick(View v) { // 点击事件的处理方法
        // 由于多个控件都把点击监听器设置到了当前页面,因此公共的onClick方法内部需要区分来自于哪个按钮
        if (v.getId() == R.id.btn_enable) { // 点击了按钮“启用测试按钮”
            btn_test.setTextColor(Color.BLACK); // 设置按钮的文字颜色
            btn_test.setEnabled(true); // 启用当前控件
        } else if (v.getId() == R.id.btn_disable) { // 点击了按钮“禁用测试按钮”
            btn_test.setTextColor(Color.GRAY); // 设置按钮的文字颜色
            btn_test.setEnabled(false); // 禁用当前控件
        } else if (v.getId() == R.id.btn_test) { // 点击了按钮“测试按钮”
            tv_result.setText("点击了"); // 设置文本视图的文本内容
        }
    }


}

 

 

 

 

 

 

 

 

 

点击:测试按钮:

 

 

 

 

点击:禁用测试按钮:

 

 

 

 

 

 

 

 

点击:启用测试按钮:

 

 

 

 

 

 

 

 

=========================================================

 

 

 

 

 

 

 

 

 

posted @ 2022-07-02 17:09  小白龙白龙马  阅读(101)  评论(0编辑  收藏  举报