按钮

kk3TWT Lv3

按钮(Button)

Button由TextView派生,其与后者有以下区别:

  1. 按钮拥有默认背景
  2. 按钮文本默认居中
  3. 按钮文本中的英文字母默认为大写(可由textAllCaps属性决定)
1
2
3
4
5
6
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a Button."
android:textSize="17sp"
android:textAllCaps="false"/>

按钮的点击事件

按钮通过两种方法来设置点击事件:

  1. 按钮的onClick属性(已过时),此时需要在activity.java中定义对应的方法

    1
    2
    3
    4
    5
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="this is a Button."
    android:onClick="setText"/>
  2. setOnClickListener() 方法,类似于Swing中的AddActionListener() 方法,这时需要实现OnClickListener接口中的onClick() 方法

    1
    2
    3
    4
    5
    6
    7
    8
    public void setText() {
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    textView.setText("the button is clicked.");
    }
    }); // 这里的匿名内部类也可以直接代替为OnClickListener的实现类
    }

对于第二种方法,也分为独立点击监听公共点击监听两种

  • 独立点击监听:这种监听器只服务于单个按钮

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class MainActivity extends AppCompatActivity {
    Button button;
    static TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.aButton);
    textView = findViewById(R.id.buttonText);
    button.setOnClickListener(new OnClickListenerExample());
    }

    static class OnClickListenerExample implements View.OnClickListener {
    @Override
    public void onClick(View v) {
    textView.setText("the button is clicked.");
    }
    }
    }
  • 公共点击监听:将整个Activity类作为OnClickListener接口的实现类,那样这个Activity中的所有按钮均能监听,适用于Activity中包含多个监听对象的情况

    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
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button abutton;
    Button bbutton;
    static TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    abutton = findViewById(R.id.aButton);
    bbutton = findViewById(R.id.bButton);
    textView = findViewById(R.id.buttonText);

    abutton.setOnClickListener(this);
    bbutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    if (v.getId() == R.id.aButton) {
    textView.setText("aButton is clicked.");
    } else if (v.getId() == R.id.bButton) {
    textView.setText("bButton is clicked.");
    }
    }
    }

按钮的长按事件

按住按钮500ms以上,点击事件就变成了长按事件

长按事件对应的接口叫做OnLongClickListener,其中包含onLongClick() 方法,但与onClick() 方法不同的是,onLongClick() 方法会返回 boolean 类型的值,表示是否消耗本次长按事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MainActivity extends AppCompatActivity {
Button abutton;
Button bbutton;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
abutton = findViewById(R.id.aButton);
bbutton = findViewById(R.id.bButton);
textView = findViewById(R.id.buttonText);

abutton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
textView.setText("abutton long click");
return false;
}
});
}
}

onLongClick() 方法返回 boolean 值的原因是,同一个屏幕上可能有多个需要使用长按事件的组件,这时需要对长按事件的发生位置作判断(判断顺序是从级别最小的组件开始,依次向上),onLongClick() 方法返回true表示“本次长按事件发生在该组件上”,此时只有该组件会响应长按事件。而方法返回false时则继续在其他组件上判断,直到所有组件都被判断或有组件响应了本次长按事件(也就是onLongClick() 方法返回了true)

按钮的禁用与恢复

在实际业务中,按钮通常拥有两种状态,即不可用状态(不允许点击,文字和背景为灰色)与可用状态(允许点击,文字正常)

按钮的可用状态使用 setEnabled() 方法设置

1
2
3
4
5
6
7
8
9
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
abutton = findViewById(R.id.aButton);

// 设置b按钮的可用状态为不可用
bbutton.setEnabled(false);
}
  • 标题: 按钮
  • 作者: kk3TWT
  • 创建于 : 2026-04-22 22:37:24
  • 更新于 : 2026-04-22 22:40:02
  • 链接: https://kk-is-very-happy.online/posts/41b27046/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
目录
按钮