在视图下方显示 toast 小部件 [英] Show toast widget underneath a view
问题描述
对于之前在这个项目上帮助过我的人,非常感谢!我的代码不再有任何问题,我做了额外的调整.现在该应用程序实际上已经很强大了,我想再做一件事.
For those who helped me out earlier regarding this project, thank you very much! My code no longer has any problems, and I made extra tweaks. Now that the app is actually robust now, I want to do one more thing.
在此处查看布局的屏幕截图.
See screenshot of the layout here.
通常,toast 视图显示在屏幕的底部中央.一旦 OnClick 被调用,我想让它出现在提交按钮的正下方(8dp).我怎样才能做到这一点.
Normally, the toast view appears at the bottom center of the screen. I want to make it appear just below (8dp) the Submit button once OnClick is called. How can I accomplish this.
在此处查看我的更新的完整项目.
package com.lookjohn.guessnumber;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Random random;
Button button;
EditText text;
int input;
int MIN, MAX;
int comp;
int guesses;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
button = (Button)findViewById(R.id.button1);
text = (EditText)findViewById(R.id.editText1);
MIN = 1;
MAX = 100;
comp = random.nextInt(MAX - MIN + 1) + MIN; // Generate random number between 1 and 100.
guesses = 0;
button.setOnClickListener(myhandler1);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// Implemented max number of guesses, detect
// number of guesses and return from the method.
String value = text.getText().toString(); // Get value from input from editTextView
// If user submits an empty EditText, return to prevent a crash.
if (value.isEmpty()) {
Toast.makeText(MainActivity.this, "You must enter a guess!", Toast.LENGTH_SHORT);
return;
}
input = Integer.parseInt(value); // Turn string into integer
guesses++;
if (input > 100) {
Toast.makeText(MainActivity.this,
"That number is greater than 100. Not Valid!",
Toast.LENGTH_SHORT).show();
return;
}
else if (input < comp)
Toast.makeText(MainActivity.this,
"Your number is too small.",
Toast.LENGTH_SHORT).show();
else if (input > comp)
Toast.makeText(MainActivity.this,
"Your number is too big.",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,
"Good job! The answer was " + comp + ".
" +
"You made " + guesses + " guesses.
" +
"Restart the app to try again.",
Toast.LENGTH_LONG).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.main, menu);
return true;
}
}
推荐答案
据我所知,这是我为该程序想到的最好的方案.这样做会将 Toast 消息置于手机中央.
From what I learned, this is the best I came up with for the program. Doing this puts the Toast message in the center of the phone.
if(input > 100) {
Toast toast = Toast.makeText(MainActivity.this,
R.string.over_guess,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
else if(input < comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_low, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(input > comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_high, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.correct, comp, guesses),
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
这篇关于在视图下方显示 toast 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!