簡體   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