按钮触控

按钮控件Button

31ede2f01e628931.png

按钮控件的新增属性

644379df00d46056.png

效果图

f80b210fc864c9d5.png

代码演示:activity_button_style.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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:text="下面的按钮英文默认大写(新版本已经不是了)"
android:gravity="center"
android:textColor="@color/black"
android:textSize="17sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello Wrold"
android:textAllCaps="true"
android:textColor="@color/black"
android:textSize="17sp"/>

<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:textAllCaps="false"
android:textColor="@color/black"
android:textSize="17sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直接指定点击方法"
android:textAllCaps="false"
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>

ButtonStyleActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.luowen.chapter03.util.DateUtil;

public class ButtonStyleActivity extends AppCompatActivity {

private TextView tv_result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_style);
tv_result = findViewById(R.id.tv_result);
//ctrl +alt +f 变成全局变量
}
public void doClick (View view){
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_result.setText(desc);
}
}

util/DateUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.luowen.chapter03.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
return sdf.format( new Date());
}
}

点击事件和长按事件

29a708ea842897d7.png

点击事件

效果图:(点击对应按钮出现)

1147d3679e8759c2.png

2fa770ad54cefe99.png

activity_button_click.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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_re"
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>

ButtonClickActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

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

import com.luowen.chapter03.util.DateUtil;

public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{

private TextView tv_re;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
tv_re = findViewById(R.id.tv_re);
Button btn_click_single = findViewById(R.id.btn_click_single);
btn_click_single.setOnClickListener(new MyOnClickListener(tv_re));
Button btn_click_public =findViewById(R.id.btn_click_public);
btn_click_public.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if(view.getId()== R.id.btn_click_public)
{
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_re.setText(desc);
}
}

static class MyOnClickListener implements View.OnClickListener{
private final TextView tv_re;

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

@Override
public void onClick(View view) {
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_re.setText(desc);
}
}
}

util/DateUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.luowen.chapter03.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
return sdf.format( new Date());
}
}

长按点击事件

效果图(长按按钮)

04e0195a41f20335.png

代码演示:activity_button_long_click.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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_resu"
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>

ButtonLongClickActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.luowen.chapter03.util.DateUtil;

public class ButtonLongClickActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_long_click);
TextView tv_resu = findViewById(R.id.tv_resu);
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_resu.setText(desc);
return true;
});
}
}

util/DateUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.luowen.chapter03.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
return sdf.format( new Date());
}
}

禁用与恢复按钮

效果图

dcbf7d726a17f7c8.png

这里默认是按钮是启用的

点击“禁用测试”按钮 (按钮就会变灰色 也就是禁用状态)

8d8bd384e61eb392.png

代码演示:

activity_button_enable.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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:enabled="false"
android:textSize="17sp"/>

<TextView
android:id="@+id/tv_res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里查看测试按钮的点击结果"
android:textColor="#000000"
android:textSize="17sp"/>
</LinearLayout>

ButtonEnableActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.luowen.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.luowen.chapter03.util.DateUtil;

public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {

private Button btn_test;
private TextView tv_res;

@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_res = findViewById(R.id.tv_res);

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

@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.GRAY);
break;

case R.id.btn_test:
String desc = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_res.setText(desc);
break;
}
}


}

util/DateUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
package com.luowen.chapter03.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf= new SimpleDateFormat("HH:mm:ss");
return sdf.format( new Date());
}
}

📖问题

在使用 switch 语句时

这里有个问题当时解决 “Android switch语句报错Constant expression required”

2ef128382eb9b0d2.png

📖原因

在Android Studio中使用JDK17以上版本,会出现switch语句报错”Constant expression required“的问题,这是因为在JDK17中switch语句的条件表达式支持使用枚举类型,而这个特性还没有被支持。

✅解决方案:添加配置

gradle.properties配置文件下添加如下代码即可解决

1
android.nonFinalResIds=false

4fd5ff811ebfc228.png

然后保存修改后,点击 Android Studio 工具栏中的 Sync Now 按钮

即可解决