简体   繁体   English

获取变量值android

[英]Get variable values android

I currently have 2 variables, iR and iL , which are defined as the number of times that the user touches the screen. 我目前有2个变量iRiL ,它们被定义为用户触摸屏幕的次数。 I define them as shown below: 我定义它们如下所示:

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

    }

What I'm having trouble with is the last two lines of code, 我遇到的麻烦是最后两行代码,

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

for some reason, the Typeface of redo and next never change. 由于某种原因, redonextTypeface永远不会改变。 I have tried moving these two lines in different places, but they still do not work. 我曾尝试将这两条线移到不同的位置,但是它们仍然无法正常工作。 Does anyone know why this is occurring? 有人知道为什么会这样吗?

Thanks in advance 提前致谢

That's because 那是因为

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

is only executed in the onCreateMethd, but not attached to the listeners. 仅在onCreateMethd中执行,而不附加到侦听器。
Try moving into the onTouch methods 尝试进入onTouch方法

The setTypeface requires a Typeface , you cannot use null . setTypeface需要一个Typeface ,不能使用null

If you want to use whatever the current Typeface is of the Button you can use the TextView#getTypeface() method. 如果要使用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);

Or, if you are using the normal font then you can use the setTypeface single-paramter method. 或者,如果您使用的是normal字体,则可以使用setTypeface单参数方法。 In which case you would provide Typeface.DEFAULT_BOLD 在这种情况下,您将提供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