gpt4 book ai didi

android - 在 View 下方显示 toast 小部件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:48 25 4
gpt4 key购买 nike

对于之前在这个项目上帮助过我的人,非常感谢!我的代码不再有任何问题,并且我进行了额外的调整。现在该应用程序实际上已经很强大了,我想再做一件事。

请参阅 layout 的屏幕截图在这里。

通常,toast View 出现在屏幕的底部中央。一旦调用 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;
}

}

最佳答案

android.widget.Toast 定义了一组有用的方法,允许自定义 toast 通知的外观和感觉。您应该研究的方法是 setGravity(int, int, int) .使用 0 偏移量,下面的代码会将 toast 顶部锚定到所提供 View 的底部,并将 toast 中心锚定到 View 的中心。

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();

关于android - 在 View 下方显示 toast 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21026786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com