简体   繁体   中英

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. 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. 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.
Try moving into the onTouch methods

The setTypeface requires a Typeface , you cannot use null .

If you want to use whatever the current Typeface is of the Button you can use the TextView#getTypeface() method.

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. In which case you would provide 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);

 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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