对于那些早些时候帮助过这个项目的人,非常感谢!我的代码不再有任何问题,我做了额外的调整。既然应用程序现在非常强大,我还想再做一件事。
在此处查看layout的屏幕截图。
通常,吐司视图显示在屏幕的底部中央。一旦调用OnClick,我想让它出现在(8dp)提交按钮下方。我怎样才能做到这一点。
在此处查看我的updated complete project。
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 + ".\n" +
"You made " + guesses + " guesses.\n" +
"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;
}
}
答案 0 :(得分:14)
android.widget.Toast 定义了一系列有用的方法,可以自定义Toast通知的外观。您应该研究的方法是setGravity(int, int, int)。如果下面有0个偏移代码,则将吐司顶部固定在所提供视图的底部,并将吐司中心固定到视图的中心。
public static void positionToast(Toast toast, View view, Window window, int offsetX, int offsetY) {
// toasts are positioned relatively to decor view, views relatively to their parents, we have to gather additional data to have a common coordinate system
Rect rect = new Rect();
window.getDecorView().getWindowVisibleDisplayFrame(rect);
// covert anchor view absolute position to a position which is relative to decor view
int[] viewLocation = new int[2];
view.getLocationInWindow(viewLocation);
int viewLeft = viewLocation[0] - rect.left;
int viewTop = viewLocation[1] - rect.top;
// measure toast to center it relatively to the anchor view
DisplayMetrics metrics = new DisplayMetrics();
window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.widthPixels, MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(metrics.heightPixels, MeasureSpec.UNSPECIFIED);
toast.getView().measure(widthMeasureSpec, heightMeasureSpec);
int toastWidth = toast.getView().getMeasuredWidth();
// compute toast offsets
int toastX = viewLeft + (view.getWidth() - toastWidth) / 2 + offsetX;
int toastY = viewTop + view.getHeight() + offsetY;
toast.setGravity(Gravity.LEFT | Gravity.TOP, toastX, toastY);
}
使用它将需要在 onClick 方法中修改与toast相关的行:
int offsetY = getResources().getDimensionPixelSize(R.dimen.toast_offset_y);
Toast toast = Toast.makeText(MainActivity.this, "That number is greater than 100. Not Valid!", Toast.LENGTH_SHORT);
positionToast(toast, v, getWindow(), 0, offsetY);
toast.show();
答案 1 :(得分:1)
关于你的问题,关于如何在特定位置展示祝酒词,你可以这样做: -
Toast toast = new Toast();
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.makeText(getApplicationContextt(),"Text",Toast.LENGTH_SHORT);
toast.show();
您可以在此代码中进行修改,以更改吐司位置: -
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
答案 2 :(得分:1)
我只想添加codebase回答。
in positionToast:
...
int toastHeight= toast.getView().getMeasuredHeight();
// compute toast offsets
...
int toastY = viewTop + (view.getHeight() - toastHeight) / 2 + offsetY;
onClick中的:
positionToast(toast, view, getWindow(), 0, 0);
无论视图放在何处,这似乎都在我的视野中完美地集中了Toast。我的代码中假设Toast总是小于它所在的视图。
答案 3 :(得分:0)
从开发者指南中,您可以使用toast.setGravity()来定位Toast。我在http://developer.android.com/guide/topics/ui/notifiers/toasts.html
上找到了这个定位你的Toast标准的Toast通知出现在附近 屏幕底部,水平居中。你可以改变这个 使用setGravity(int,int,int)方法的位置。这接受三个 参数:重力常数,x位置偏移和y位置 偏移量。
例如,如果你决定吐司应该出现在 左上角,您可以像这样设置重力:
toast.setGravity(Gravity.TOP | Gravity.LEFT,0,0);如果你想轻推 向右的位置,增加第二个参数的值。 要轻推它,请增加最后一个参数的值。
我认为您可以获得按钮的位置并将其转发给上述方法
答案 4 :(得分:0)
据我所知,这是我为该计划提出的最好的。这样做会将Toast消息放在手机的中央。
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();
}
答案 5 :(得分:0)
没有任何答案对我有用,我将上述答案组合在一起,我得到了解决方案。
第二个计时器是目标视图。
public static void displayToastUnderView(Activity activity, View view, String text) {
Toast toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
toast.setGravity( Gravity.TOP, view.getLeft() - view.getWidth() / 2 - toast.getView().getWidth() / 2, view.getBottom());
toast.show();
}