我无法理解为什么我的吐司不会出现任何人可以帮助...我今天开始学习应用程序开发但我不明白这是什么问题... 我的应用程序没有任何错误,这是我的编程书中的工作练习
package com.example.geoquiz;
import android.widget.Button;
import android.widget.Toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class QuizActivity extends Activity {
private Button but1;
private Button but2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
but1 = (Button) findViewById(R.id.true_button);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
but2 = (Button) findViewById(R.id.false_button);
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this, R.string.incorect_toast, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"
android:id ="@+id/true_button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"
android:id ="@+id/false_button" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GeoQuiz</string>
<string name="hello_world">Hello world!</string>
<string name ="question_text">Gabriel este un baiat destept ?</string>
<string name ="true_button">TRUE</string>
<string name ="false_button">FALSE</string>
<string name ="action_settings">Settings</string>
<string name ="correct_toast">RaspunsCorect</string>
<string name ="incorect_toast">RaspunsIncorect</string>
</resources>
答案 0 :(得分:0)
试试这个。进入你的xml,然后在你要制作吐司文字的按钮上添加
android:onClick="ToastButtonClicked" />
^进入它。
然后使用以下代码制作吐司文本。
}
public void ToastButtonClicked(View view)
{
Toast.makeText(this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:-1)
尝试替换
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
与
Toast.makeText(getApplicationContext(),R.string.correct_toast, Toast.LENGTH_SHORT).show();
答案 2 :(得分:-1)
替换此
Toast.makeText(QuizActivity.this,R.string.correct_toast, Toast.LENGTH_SHORT).show();
与
Toast.makeText(QuizActivity.this,"Correct Toast", Toast.LENGTH_SHORT).show
();
可能会帮助你