提问者:小点点

吐司留言保留时间长


我想在搜索后没有找到结果的时候展示吐司,我做了一个简单的吐司,但是吐司留了很长时间,我想在短时间后它就消失了

这是代码

            public boolean onQueryTextSubmit(String query) {

            final String[] dose = getResources().getStringArray(R.array.doses);
            final String[] use = getResources().getStringArray(R.array.uses);
            final String[] composition = getResources().getStringArray(R.array.composition);
            
            final TypedArray img  = getResources().obtainTypedArray(R.array.img);
            for (String search : suggestions)
            {
                if (query.equals(search)){

                    for (int i =0; i<suggestions.length; i++){

                        if (query.equals(suggestions[i])){
                            Intent intent = new Intent(MainActivity.this,productDetails.class);

                            int im = img.getResourceId(i,-1);

                            intent.putExtra("img",im);

                            intent.putExtra("dose",dose[i]);
                            intent.putExtra("use",use[i]);
                            intent.putExtra("composition",composition[i]);
                            intent.putExtra("title",suggestions[i].toUpperCase());

                            startActivity(intent);
                        }
                        }
                }
              else if (query!= search){
                    Toast.makeText(MainActivity.this, "No Results Found", Toast.LENGTH_SHORT).show();
                    
                }

            }

            return false;
        }

共1个答案

匿名用户

您的toast显示在for循环中,他被添加到队列中多次,将其从循环中删除