5.25

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

今日總結

學習時間1h

package com.app.chapter03;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.app.chapter03.utils.DateUtil;

/** @noinspection unused*/
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener {

private TextView tv_result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_button_click);
Button btn_click_single = findViewById(R.id.btn_click_single);
tv_result = findViewById(R.id.tv_result);
btn_click_single.setOnClickListener(new MyOnClickListener(tv_result));

Button btn_click_public = findViewById(R.id.btn_click_public);
btn_click_public.setOnClickListener(this);


}

/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_click_public){
String desc =String.format("%s 您點選了按鈕: %s",DateUtil.getNowTime(), ((Button)v).getText());
tv_result.setText(desc);

}
}

static class MyOnClickListener implements View.OnClickListener{

private final TextView tv_result;

public MyOnClickListener(TextView tv_result) {
this.tv_result = tv_result ;
}

/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
String desc =String.format("%s 您點選了按鈕: %s",DateUtil.getNowTime(), ((Button)v).getText());
tv_result.setText(desc);
}
}