[英]Toast Message Not Working android app eclipse
Toast 消息仍然沒有任何幫助,運行正常,但是當沒有輸入數字並單擊按鈕時,應用程序崩潰並說不幸的是已停止工作。 只是想讓我的應用程序更防崩潰,讓用戶享受更多
代碼:
公共類 MainActivity 擴展 Activity {
public static final String TAG = MainActivity.class.getCanonicalName();
TextView textOne;
TextView guessText;
EditText userGuess;
Button pushMe;
MediaPlayer applause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
varSet();
pushMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.w(TAG, "Button Clicked");
String randText = "";
// TODO Auto-generated method stub
Random randGen = new Random();
int rando = randGen.nextInt(10) +1;
if((guessText.getText().toString()).length() == 0)
{
Toast.makeText(getApplicationContext(), "no number entered", Toast.LENGTH_SHORT).show();
}
else{
int userNumber = Integer.parseInt(userGuess.getText().toString());
if ( userNumber < 1 || userNumber > 10 ) {
guessText.setText("Please guess 1 - 10");
} else if ( userNumber == rando) {
guessText.setText("You Got It Right!");
if (applause.isPlaying()) {
applause.seekTo(0);
} else {
applause.start();
}
} else {
guessText.setText("Not quite, guess again!");
}
randText = Integer.toString(rando);
textOne.setText(randText);
userGuess.setText("");
}
}});
}
private void varSet() {
textOne = (TextView) findViewById(R.id.textView1);
guessText = (TextView) findViewById(R.id.textView2);
userGuess = (EditText) findViewById(R.id.editText1);
pushMe = (Button) findViewById(R.id.button1);
applause = MediaPlayer.create(MainActivity.this, R.raw.applause);
}
謝謝你會很棒!!!
如果您想添加 Toast 消息,請查看 Android 開發者指南http://developer.android.com/guide/topics/ui/notifiers/toasts.html (這是開始所有 Android 開發相關的好地方問題)
這一切都始於這樣的事情:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
在您的 onClick 方法中,您可以這樣寫:
OnClickListener buttonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if((guessText.getText().toString()).length() == 0)
{
Toast.makeText(getApplicationContext(), "no number entered", Toast.LENGTH_SHORT).show();
}
else{
int userNumber = Integer.parseInt(userGuess.getText().toString());
if ( userNumber < 1 || userNumber > 10 ) {
guessText.setText("Please guess 1 - 10");
} else if ( userNumber == rando) {
guessText.setText("You Got It Right!");
if (applause.isPlaying()) {
applause.seekTo(0);
} else {
applause.start();
}
} else {
guessText.setText("Not quite, guess again!");
}
randText = Integer.toString(rando);
textOne.setText(randText);
userGuess.setText("");
}
}
};
pushMe.setOnClickListener(buttonClickListener);
試試下面的代碼:-
showToast(this, "your message");
public static void showToast(Activity activity, String msg)
{
try
{
Toast toast = Toast.makeText(activity, msg, Toast.LENGTH_LONG);
toast.getView().setBackgroundColor(activity.getResources().getColor(R.color.purple_dark));
toast.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.