繁体   English   中英

获取变量值android

[英]Get variable values android

我目前有2个变量iRiL ,它们被定义为用户触摸屏幕的次数。 我定义它们如下所示:

public class Touchscreen extends Activity {

int iL;
int iR;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touchscreen);

        final View touchLayoutL = findViewById(R.id.touchLayoutL);
        final View touchLayoutR = findViewById(R.id.touchLayoutR);
        final Button redo = (Button)findViewById(R.id.redo);
        final Button next = (Button)findViewById(R.id.next);

        iL=1;
        iR=1;

        touchLayoutL.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if (event.getAction()==(MotionEvent.ACTION_DOWN)){
                    iL++;
                }
        });

        touchLayoutR.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if (event.getAction()==(MotionEvent.ACTION_DOWN)){
                    iR++;
                }
        });

        if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
        if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);

    }

我遇到的麻烦是最后两行代码,

        if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
        if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);

由于某种原因, redonextTypeface永远不会改变。 我曾尝试将这两条线移到不同的位置,但是它们仍然无法正常工作。 有人知道为什么会这样吗?

提前致谢

那是因为

    if (iL>1 || iR>1) redo.setTypeface(null, Typeface.BOLD);
    if (iL>3 && iR>3) next.setTypeface(null, Typeface.BOLD);

仅在onCreateMethd中执行,而不附加到侦听器。
尝试进入onTouch方法

setTypeface需要一个Typeface ,不能使用null

如果要使用Button当前的Typeface是什么,可以使用TextView#getTypeface()方法。

if (iL>1 || iR>1) redo.setTypeface(redo.getTypeface(), Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(next.getTypeface(), Typeface.BOLD);

或者,如果您使用的是normal字体,则可以使用setTypeface单参数方法。 在这种情况下,您将提供Typeface.DEFAULT_BOLD

if (iL>1 || iR>1) redo.setTypeface(Typeface.DEFAULT_BOLD);
if (iL>3 && iR>3) next.setTypeface(Typeface.DEFAULT_BOLD);

/*
 * Same as above

if (iL>1 || iR>1) redo.setTypeface(Typeface.NORMAL, Typeface.BOLD);
if (iL>3 && iR>3) next.setTypeface(Typeface.NORMAL, Typeface.BOLD);

 */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM