[英]Can't view my TextView inside buttons onClickListener
大家,早安,
我想找出為什么我的textview不會出現在我的應用程序中。 我在按鈕的onClick方法中聲明了它們,所以就像點擊按鈕一樣,彈出它們。 它基本上用於“輸入”類型的按鈕,如果用戶輸入正確答案,則在textview中顯示正確,否則錯誤。
public void Easy12(){
Random rand = new Random();
int a = (int) rand.nextInt(100)+1;
int b = (int) rand.nextInt(100)+1;
final TextView tv = (TextView) findViewById(R.id.question_label);
String aString = Integer.toString(a);
String bString = Integer.toString(b);
String display = aString + " + " + bString + " =";
tv.setText(display);
final int c = a + b;
final Button buttonhash = (Button) findViewById(R.id.keypad_hash);
buttonhash.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
final EditText edittext = (EditText) findViewById(R.id.USERentry);
if(edittext.getText().equals(c)){
final TextView answerLabel = (TextView) findViewById(R.id.rightwrong_label);
answerLabel.setText(R.string.answer_correct);
answerLabel.setTextColor(R.color.correct_color);
}
else
{
final TextView answerLabel = (TextView) findViewById(R.id.rightwrong_label);
answerLabel.setText(R.string.answer_wrong);
answerLabel.setTextColor(R.color.wrong_color);
}
}
});
}
我不得不制作單獨的文本視圖來調用相同的textview,因為括號不會識別它們。 這個方法是我的Game類的一部分,它只顯示一個表達式,並通過按屏幕上的a按鈕檢查用戶的回答是否正確。 如果有人知道什么是錯的,請分享。 謝謝
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/rightwrong_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/USERentry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textColor="@color/user_txt_color"/>
<TableLayout....
這是因為您的View沒有得到更新,您正在編寫textViews。 嘗試通過重新繪制視圖來刷新視圖,或者通過一些意圖來查看寫入部分是否正常工作
或者,如果你有興趣使用“TOAST”方法,
Toast.makeText(LoginpageActivity.this, "Write your text here to display", Toast.LENGTH_LONG).show();
這里,“LoginpageActivity”是當前的類名
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.