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

7

我在布局中有一个文本视图。我的要求是当我按下或聚焦它时,文本应该加粗。否则它应该使用普通字体。我该如何实现它?

2个回答

14

使用以下代码

TextView name=((TextView)findViewById(R.id.TextView01));


name.hasFocus();
        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(hasFocus)
                    ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD);
                else
                    ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT);

            }
        });

        name.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                ((TextView)findViewById(R.id.TextView01)).setTypeface(Typeface.DEFAULT_BOLD);
                return false;

});

当我给出这样的代码时,文本会变成粗体。但是在将其加粗后,如何将焦点从它上面移开?实际上,在单击该TextView时,我正在调用电子邮件意图。如果我按设备的返回按钮,则应该取消加粗并恢复为正常样式。我该怎么做呢? - Anju
嗨,我搞定了。我只提供了setOnTouchListener代码部分。我的电子邮件意图代码是写在另一个方法中的。在那里,我给出了默认字体。因此它满足了我的要求。感谢您的帮助。 - Anju

1
我实际上创建了一个小类来使这个过程更加容易:ClickTextView。你可以将它复制粘贴到你的项目中,Android Studio 应该会自动修复它。
public class ClickTextView extends android.support.v7.widget.AppCompatTextView {

    Rect rect;

    public ClickTextView(Context context) {
        super(context);
        init();
    }

    public ClickTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClickTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setClickable(true);
        rect = new Rect();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_HOVER_ENTER){
            this.setTypeface(Typeface.DEFAULT_BOLD);
            getHitRect(rect);
        }else if (action == MotionEvent.ACTION_MOVE){
            boolean inView = rect.contains(getLeft() + (int) event.getX(), getTop() + (int) event.getY());
            this.setTypeface(inView ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
        }else if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_CANCEL
                || action == MotionEvent.ACTION_HOVER_EXIT
                || action == MotionEvent.ACTION_OUTSIDE) {
            this.setTypeface(Typeface.DEFAULT);
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        if (focused) this.setTypeface(Typeface.DEFAULT_BOLD);
        else this.setTypeface(Typeface.DEFAULT);
    }
}

它还提供了一个解决方案,以防用户点击您的视图,但将手指拖到视图边界之外。如果在那里释放,Android 将不会注册点击事件,因此文本也不应该加粗。

随意在任何地方使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接