2.27

安卓组件按钮

package com.example.chapter02;

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;

public class ButtonEnable extends AppCompatActivity implements View.OnClickListener {

    private Button btn_test;
    private TextView TV;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_enable);

        TV =findViewById(R.id.tv_result);

        Button button02 =findViewById(R.id.button02);
        Button button03 =findViewById(R.id.button03);
        btn_test  =findViewById(R.id.button04);

        button02.setOnClickListener(this);
        button03.setOnClickListener(this);
        btn_test.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.button02:
//                btn_test.setEnabled(true);
//                btn_test.setTextColor(Color.BLACK);
//                break;
//            case R.id.button03:
//                btn_test.setEnabled(false);
//                btn_test.setTextColor(Color.GRAY);
//                TV.setText("按钮已禁用");
//                break;
//            case R.id.button04:
//                TV.setText("按钮已启用了");
//                break;
//        }
        if (v.getId()==R.id.button02){
            btn_test.setEnabled(true);
            btn_test.setTextColor(Color.BLACK);
        } else if (v.getId()==R.id.button03) {
            btn_test.setEnabled(false);
            btn_test.setTextColor(Color.GRAY);
            TV.setText("按钮已禁用");
        } else if (v.getId()==R.id.button04) {
            TV.setText("按钮已启用了");
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/button02"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="开启测试按钮"
            android:textColor="#ff0000"
            android:layout_margin="10dp"
            />

        <Button
            android:id="@+id/button03"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="禁用测试按钮"
            android:layout_margin="10dp"
            android:layout_weight="1"
            />


    </LinearLayout>

    <Button
        android:id="@+id/button04"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按钮"
        android:textColor="#888888"
        android:enabled="false"
        />
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看测试结果"
        android:gravity="center"
        />
</LinearLayout>
posted @ 2024-02-27 19:55  aallofitisst  阅读(18)  评论(0)    收藏  举报