首页 > 其他分享 >每日总结 3.10

每日总结 3.10

时间:2023-03-10 20:13:26浏览次数:32  
标签:总结 3.10 每日 Button import android btn id view

今天学习了按键的操作。

首先是按键的点击事件:

package com.example.dongnao;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.BreakIterator;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView textView;
    private TextView textview2;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_result);
        Button button=findViewById(R.id.btn_click_single);
        Button button1=findViewById(R.id.btn_long_click);
        textview2=findViewById(R.id.tv_results);
        button.setOnClickListener(this);
        button1.setOnLongClickListener(view -> {
                    String desc=String .format("%s 你点击了按钮:%s", DateUtil.getNowTime(),((Button) view).getText());
                    textview2.setText(desc);
           return true;
        }
        );

    }
    public  void onClick(View view){
        if(view.getId()==R.id.btn_click_single){
            String desc=String .format("%s 你点击了按钮:%s", DateUtil.getNowTime(),((Button) view).getText());
            textView.setText(desc);
        }
     }
    /*static class MyOnClickListener implements View.OnClickListener{
        private final TextView textView;

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

        @Override
        public void onClick(View view) {
            String desc=String .format("%s 你点击了按钮:%s", DateUtil.getNowTime(),((Button) view).getText());
            textView.setText(desc);
        }
    }*/
}
<?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"
    tools:context=".MainActivity"
    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"
        />
    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:text="查看结果"
        android:textColor="#000000"
        android:textSize="15sp"
        />
    <Button
        android:id="@+id/btn_long_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="长按指定监听"
        android:textColor="#000000"
        android:textSize="15sp"
        />
    <TextView
        android:id="@+id/tv_results"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:text="查看结果"
        android:textColor="#000000"
        android:textSize="15sp"
        />


</LinearLayout>

而后是按键的启用和禁用:

package com.example.dongnao;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_enable);
        Button btn_enable= findViewById(R.id.btn_enable);
        Button btn_disable= findViewById(R.id.btn_disable);
        btn_test = findViewById(R.id.btn_test);
        TextView tv_result=findViewById(R.id.tv_resultse);


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

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_enable:
                btn_test.setEnabled(true);
                btn_test.setTextColor(Color.BLACK);
                break;
            case R.id.btn_disable:
                btn_test.setEnabled(false);
                btn_test.setTextColor(Color.GREEN);
                break;
        }
    }
}
<?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="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="17dp"
            />
        <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="17dp"
            />

    </LinearLayout>
    <Button
        android:id="@+id/btn_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按钮"
        android:textColor="#888888"
        android:textSize="17dp"
        android:enabled="false"
        />

    <TextView
        android:id="@+id/tv_resultse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看点击结果"/>

</LinearLayout>

 

标签:总结,3.10,每日,Button,import,android,btn,id,view
From: https://www.cnblogs.com/syhxx/p/17204540.html

相关文章

  • 每日学习总结_20230310
    今天学习了Android开发的基础知识,包括活动(Activity)、布局(Layout)、视图(View)等概念。还学习了如何创建一个简单的界面,使用XML进行布局,以及如何在Java代码中处理用户输入。明......
  • mysql锁总结
    概述:锁是在并发访问时,解决数据的有效性、一致性问题,有全局锁、表级锁、行级锁,锁粒度越小越好。全局锁:是对整个数据库实例加锁,一旦对整个数据库实例加了锁,那么就意味着这个......
  • 每日总结2023/3/10
    今天学习了AS中的复选框按钮效果如下  tv_text=findViewById(R.id.tv_text);bt_fa=findViewById(R.id.bt_fa);cb_sing=findViewById(R.id......
  • 2023.3.10
    这几天讲的倍增捏(一开始以优化dp为主,现在几乎纯倍增,6)。然后发现我不会快速幂。LuoguP1226.【模板】快速幂||取余运算 传送门我们知道任意整数可以表示成若干个2的......
  • 3.10 滴水复习6
    1.全局变量与局部变量全局变量程序启动即分配地址一直在不销毁局部变量没有固定地址所在函数体结束后会被销毁2.函数参数根据内外平栈代码查看问题寄存器传......
  • 记录--vue3+setup+ts 知识总结
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助vue3于2020年09月18日正式发布,2022年2月7日vue3成为新的默认版本距离vue3正式发布已......
  • 3月10日总结
    本文介绍基于Python中GDAL模块,实现MODIS遥感影像数据的读取、计算,并基于质量控制QC波段进行图像掩膜的方法。前期的文章PythonGDAL读取栅格数据并基于质量评估波段QA对指......
  • Python - else 语法总结
    else使用汇总。问题阅读别人代码,有点疑惑,精简后如下:defcode_example(arg=None):foriinrange(5):ifarg:breakelse:pr......
  • Java之BigDecimal 使用总结
     一、BigDecimal 产生   Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际......
  • 3.10每日总结
    今天学习了开关按钮ToggleButton和开关开关。Android基本UI控件是:开关按钮ToggleButton和开关Switchandroid:disabledAlpha:设置按钮在禁用时的透明度android:textOff:按钮......