04每周总结

04个人总结

发表时间:23.3.14

这周我学习了android按钮的使用,对于按钮的使用有了详细的了解
package com.dongnaoedu.chapter03;

 

import androidx.appcompat.app.AppCompatActivity;

 

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

 

import com.dongnaoedu.chapter03.util.DateUtil;

 

public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener {

 

    private TextView tv_result;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_button_click);

        tv_result = findViewById(R.id.tv_result);

        Button btn_click_single = findViewById(R.id.btn_click_single);

        btn_click_single.setOnClickListener(new MyOnClickListener(tv_result));

 

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

        btn_click_public.setOnClickListener(this);

    }

 

    @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;

        }

 

        @Override

        public void onClick(View v) {

            String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(), ((Button) v).getText());

            tv_result.setText(desc);

        }

    }

}

  

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <Button

        android:id="@+id/btn_click_single"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="指定单独的点击监听器"

        android:textColor="#000000"

        android:textSize="15sp"/>

 

    <Button

        android:id="@+id/btn_click_public"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="指定公共的点击监听器"

        android:textColor="#000000"

        android:textSize="15sp"/>

 

    <TextView

        android:id="@+id/tv_result"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:padding="5dp"

        android:gravity="center"

        android:textColor="#000000"

        android:textSize="15sp"

        android:text="这里查看按钮的点击结果"/>

 

</LinearLayout>

 

 

 

 

posted @   樱花开到我身边  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示