Android如何在按下或聚焦时使按钮文本加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2682051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:55:33  来源:igfitidea点击:

Android how to make button text bold when pressed or focussed

android

提问by stealthcopter

I want to change the text inside a button to be bold when the button is highlighted or pressed. I currently use a xml file to define the button and use the XML to change how it looks when pressed but I would like to do this without using an image.

我想在突出显示或按下按钮时将按钮内的文本更改为粗体。我目前使用 xml 文件来定义按钮并使用 XML 来更改按下时的外观,但我想在不使用图像的情况下执行此操作。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
          android:state_pressed="false" 
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="true" 
          android:state_pressed="true"
          android:drawable="@drawable/reset_hover" />
    <item android:state_focused="false" 
          android:state_pressed="true"
      android:drawable="@drawable/reset_hover" />
    <item android:drawable="@drawable/reset" />
</selector>

I tried using something like the following, but it doesn't seem to ever get called.

我尝试使用类似下面的东西,但它似乎从来没有被调用过。

    final Button btn_reset = (Button) findViewById(R.id.btn_reset);
    btn_reset.setOnClickListener(this); 
    btn_reset.setOn(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){btn_reset.setTypeface(null, Typeface.BOLD);}
        else{btn_reset.setTypeface(null, Typeface.NORMAL);}
    }
   });

采纳答案by Henry

You could create your own style.xml in which you define the text style. In your selector you can reference to the style. style.xml

您可以创建自己的 style.xml,在其中定义文本样式。在您的选择器中,您可以参考样式。样式文件

<style name="myStyle">  
    <item name="android:textSize">9px</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textStyle">bold</item>
</style>

And in your selector

在你的选择器中

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
      android:state_pressed="false" 
      style="@style/myStyle" /> </selector>

回答by DEzra

You could try putting the bold code inside the click event for the button:

您可以尝试将粗体代码放在按钮的点击事件中:

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Set bold on click
                 button.setTypeface(null, Typeface.BOLD);
             }
         });

回答by Faraz

Styles are not allowed in selectors. Reference

选择器中不允许使用样式。参考



And to make the text bold, use this code:

要使文本加粗,请使用以下代码:

btn_reset.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            // When the user clicks the Button
            case MotionEvent.ACTION_DOWN:
                btn_reset.setTypeface(Typeface.DEFAULT_BOLD);
                break;

            // When the user releases the Button
            case MotionEvent.ACTION_UP:
                btn_reset.setTypeface(Typeface.DEFAULT);
                break;
        }
        return false;
    }
});