Android how to make button text bold when pressed or focussed

后端 未结 3 1202
执念已碎
执念已碎 2020-12-08 21:28

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

3条回答
  •  粉色の甜心
    2020-12-08 22:08

    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;
        }
    });
    

提交回复
热议问题