5.26

南笙西瓜發表於2024-06-16

今日總結

學習時間2h致力於android的學習 無法自拔

package com.app.chapter03;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.app.chapter03.utils.DateUtil;

public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

private Button btn_text;
private TextView tv_result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_button_enable);

Button btn_enable = findViewById(R.id.btn_enable);
Button btn_disable = findViewById(R.id.btn_disable);
tv_result = findViewById(R.id.tv_result);
Button btn_text = findViewById(R.id.btn_text);

btn_enable.setOnClickListener(this);
btn_disable.setOnClickListener(this);
btn_text.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Button btn_text = findViewById(R.id.btn_text);
if (v.getId() == R.id.btn_enable) {
btn_text.setEnabled(true);
btn_text.setTextColor(Color.BLACK);
} else if (v.getId() == R.id.btn_disable) {
btn_text.setEnabled(false);
btn_text.setTextColor(Color.GRAY);
}if(v.getId() == R.id.btn_text){
String desc =String.format("%s 您點選了按鈕: %s", DateUtil.getNowTime(), ((Button)v).getText());
tv_result.setText(desc);
}
}
}