2022-10-12学习内容
1.长按点击事件
1.1activity_button_long_click.xml
<?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_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_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>
1.2ButtonLongClickActivity.java
package com.example.chapter03; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.example.chapter03.utils.DateUtil; public class ButtonLongClickActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_long_click); TextView tv_result = findViewById(R.id.tv_result); Button btn_long_click = findViewById(R.id.btn_long_click); btn_long_click.setOnLongClickListener(v -> { String desc = String.format("%s 您点击了按钮; %s", DateUtil.getNowTime(), ((Button)v).getText()); tv_result.setText(desc); return true; }); } }
1.3效果
2.禁用与恢复按钮
2.1activity_button_enable.xml
<?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"> <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_weight="1" android:layout_height="wrap_content" android:text="启用测试按钮" android:textColor="#000000" android:textSize="17sp"/> <Button android:id="@+id/btn_disable" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="禁用测试按钮" android:textColor="#000000" android:textSize="17sp"/> </LinearLayout> <Button android:id="@+id/btn_test" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="测试按钮" android:textColor="#888888" android:textSize="17sp" android:enabled="false"/> <TextView android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这里查看测试按钮的点击结果" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
2.2ButtonEnableActivity.java
package com.example.chapter03; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.example.chapter03.utils.DateUtil; public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_test; private TextView tv_result; @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); tv_result = findViewById(R.id.tv_result); btn_enable.setOnClickListener(this); btn_disable.setOnClickListener(this); btn_test.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.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.GRAY); break; case R.id.btn_test: String desc = String.format("%s 您点击了按钮; %s", DateUtil.getNowTime(), ((Button)v).getText()); tv_result.setText(desc); break; } } }
2.3效果