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("点击了"); // 设置文本视图的文本内容
        }
    }


}

点击:测试按钮:

点击:禁用测试按钮:

点击:启用测试按钮:

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值