按钮控件Button
按钮控件Button由TextView派生而来,它们之间的区别有:
Button拥有默认的按钮背景,而TextView默认无背景;
Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;
Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;
对比一下Botton和TextView
<?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"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="下面的按钮英文默认大写"
android:textColor="@color/black"
android:textSize="17sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/black"
android:textSize="17sp"/>
</LinearLayout>
先在MainActivity建一个EmptyActivity命名为ButtonStyleActivity
<?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"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="下面的按钮英文保持原状"
android:textColor="@color/black"
android:textSize="17sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/black"
android:textSize="17sp"
android:textAllCaps="false"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直接指定点击方法"
android:textColor="@color/black"
android:textSize="17sp"
android:onClick="doClick"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里查看按钮的点击结果"
android:textColor="@color/black"
android:textSize="17sp"/>
</LinearLayout>
DateUtil.java
package com.example.myapplication;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf=new android.icu.text.SimpleDateFormat(pattern:"HH,mm,ss");
return sdf.format(new Date());
}
}
点击事件
监听器,是专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。
点击监听器,setOnClickListener方法设置。按钮被按住少于500毫秒时,会触发点击事件。
长按监听器,setOnLongClickListener方法设置。按钮被按住超过500毫秒时,会触发长按事件。
因为该按钮要设置监听。所以要先给他一个id
<?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"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:textSize="15sp"
android:text="这是查看按钮的点击结果"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonClickActivity extends AppCompatActivity {
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 MyOnClickListerner(tv_result));
}
static class MyOnClickListerner implements View.OnClickListener {
//创建了这个类 实现了接口 实例化这个类
//static 减少内存泄漏
private final TextView tv_result;
public MyOnClickListerner(TextView tv_result){
this.tv_result=tv_result;
}
@Override
public void onClick(View view) {
String desc =String.format("%s 您点击了按钮: %s,DateUtil.getNowTime(),((Button)view).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_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:textSize="15sp"
android:text="这是查看按钮的点击结果"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonLongClickActivity extends AppCompatActivity {
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
@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(view ->{
String desc =String.format("%s 您点击了按钮: %s,DateUtil.getNowTime(),((Button)view).getText()");
tv_result.setText(desc);
return true;
});
}
}
禁用与恢复按钮
<?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_text"
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>
package com.example.myapplication;
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;
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_text;
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_text = findViewById(R.id.btn_text);
tv_result = findViewById(R.id.tv_result);
btn_enable.setOnClickListener(this);
btn_disable.setOnClickListener(this);
btn_text.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_enable:
//启用当前控件
btn_text.setEnabled(true);
//设置按钮的文字颜色
btn_text.setTextColor(Color.BLACK);
break;
case R.id.btn_disable:
//禁用当前控件
btn_text.setEnabled(false);
btn_text.setTextColor(Color.GRAY);
break;
case R.id.btn_text:
String desc =String.format("%s 您点击了按钮: %s,DateUtil.getNowTime(),((Button)view).getText()");
tv_result.setText(desc);
break;
}
}
}