每日总结 3.10
今天学习了按键的操作。
首先是按键的点击事件:
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>