[英]Get variable values android
我目前有2個變量iR
和iL
,它們被定義為用戶觸摸屏幕的次數。 我定義它們如下所示:
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);
由於某種原因, redo
和next
的Typeface
永遠不會改變。 我曾嘗試將這兩條線移到不同的位置,但是它們仍然無法正常工作。 有人知道為什么會這樣嗎?
提前致謝
那是因為
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.