android - 如何将 Toast 的动态位置设置为 View ?

标签 android events checkbox toast

首先这不是完整的代码

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); 
        View view=toast.getView();
        switch(buttonView.getId())
        {
            case R.id.chkRed: if(chkRed.isChecked())
            {   
                              chkGreen.setChecked(false);
                              chkBlue.setChecked(false);
                              chkYellow.setChecked(false);
                              toast.setText("You Selected Red");
                                toast.setGravity(Gravity.NO_GRAVITY,buttonView.getRight(), chkRed.getTop());
                             //top does not align
                             //it align perfectly to right of checkbox
                              view.setBackgroundColor(Color.RED);
            }
                             break;
}
}

所以现在的问题是我想在复选框旁边显示 toast,我尝试使用 setGravity(),但它只是不起作用并且已经被击中并尝试了很长时间,但是没有进展

如何在复选框旁边显示toast?

最佳答案

好的,我终于想出了如何在复选框或其他 View 旁边 toast

1.您需要获取 View 在屏幕上的位置,通过

buttonView.getLocationOnScreen(location);

有关更多引用,请参阅 getLocationOnScreen(int[])

2.使用

设置重力 toast
toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, 
location[1]-10);

这里重要的是将 toast 的 x-coordinate 设置到 View 的右侧, 例如,buttonView.getRight() 并从 location[1] 获取 y 坐标,您从 getLocationOnScreen()

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        Toast toast=Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); 
        View view=toast.getView();
        int location[]=new int[2];
        buttonView.getLocationOnScreen(location);
        switch(buttonView.getId())
        {
            case R.id.chkRed: if(chkRed.isChecked())
            {   
                              chkGreen.setChecked(false);
                              chkBlue.setChecked(false);
                              chkYellow.setChecked(false);
                              toast.setText("You Selected Red");
                              toast.setGravity(Gravity.TOP|Gravity.LEFT,buttonView.getRight()+5, location[1]-10);
                              view.setPadding(1, 1, 1, 1);
                              view.setBackgroundColor(Color.RED);
            }

        }
    }

关于android - 如何将 Toast 的动态位置设置为 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460269/

相关文章:

android - Activity 会运行,但不会显示(Android)?

java - 元素类型 "provider"必须后跟属性规范 ">"或 "/>"

javascript - 为什么使用模板时不会出现 Treeview 复选框?

php - 在 php 代码中插入复选框

android - 在 android 中更新 SQLite 表的问题

安卓应用内订阅 : Inventory call returns purchase as null even if purchase exists

javascript - 如何在 QWebView 中通过 C++ 触发 JavaScript 事件

c# - 捕获控制台退出 C#

events - Blazor 组件 : How to communicate from grandchild to child to parent or grandchild to parent

javascript - Angular 复选框未绑定(bind)到模型