【问题标题】:Toast Message will not display if placed at the beggining of the try catch如果放置在 try catch 的开头,则不会显示 Toast 消息
【发布时间】:2020-01-29 20:46:00
【问题描述】:

我在显示 Toast 消息时遇到问题。我希望它在最初单击按钮时显示,以便用户知道应用程序正在运行(在我的情况下,生成 QR 码)。如果我将吐司放在 try catch 的开头,单击按钮时它不会立即显示。只有在生成并显示二维码后才会显示吐司。此外,如果我将吐司放在 try catch 的开头,则会在我的 Android 模拟器上显示(在显示 QR 码之后),但在我的物理 android 设备上则根本不显示(我已允许应用程序权限)。如果 toast 在 try catch 结束时,它只会显示在我的物理设备上。这让我很困惑。我尝试使用将在按钮单击时运行并在显示 QR 码后终止的 ProgressBar,但发现这很难实现。我认为吐司将是一个简单的选择,因为我在整个项目中都使用了它们,直到现在没有任何问题。如果有人可以帮助我了解如何在单击按钮时立即显示吐司,而不是在生成和显示 QR 码后显示,我将不胜感激。或者,如果有人可以帮助我包括一个进度条(圆圈),一旦单击按钮就运行并在显示 QR 码后停止,这也将非常感激,因为我相信这将是最理想的解决方案。

请看下面的代码:

公共类生成扩展 AppCompatActivity {

public final static int QRcodeWidth = 500 ;
private static final String IMAGE_DIRECTORY = "/QR_Code_Generated";
Bitmap bitmap;
private EditText etQr;
private ImageView ivGenerated;
private Button btn;
private Button clrBtn;
private Button shareBtn;
private ProgressBar progressBar;
private int progressStatus = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_generate);

    ivGenerated = (ImageView) findViewById(R.id.iv_generated);
    etQr = (EditText) findViewById(R.id.generate_input);
    btn = (Button) findViewById(R.id.button);
    clrBtn = (Button) findViewById(R.id.clearButton);
    shareBtn = (Button) findViewById(R.id.shareButton);



    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etQr.getText().toString().trim().length() == 0) {

            } else {

                try {
                    Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
                    toast1.show();

                    bitmap = TextToImageEncode(etQr.getText().toString());
                    ivGenerated.setImageBitmap(bitmap);
                    String path = saveImage(bitmap);  //give read write permission

                    // Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)


                } catch (WriterException e) {
                    e.printStackTrace();
                }


            }
        }
    });

【问题讨论】:

    标签: android-studio try-catch android-toast


    【解决方案1】:

    我没有发现您的 toast 弹出代码有任何问题,您可以尝试在 toast.show() 之后添加 250 毫秒的延迟,也许可以。对你来说是一个解决方法。

    对于进度条,它非常简单。在您的 .xlm 文件中创建一个进度条,并将其放在您愿意显示它的 View.Layout 的中心并使其可见性为 FALSE。 你可以试试这个,

    progressbar = = (ProgressBar) findViewById(R.id.progressbar);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressbar.setVisibility(VISIBLE); // or try View.VISIBLE
            if (etQr.getText().toString().trim().length() == 0) {
    
            } else {
    
                try {
                    Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
                    toast1.show();
    
                    bitmap = TextToImageEncode(etQr.getText().toString());
                    ivGenerated.setImageBitmap(bitmap);
                    String path = saveImage(bitmap);  //give read write permission
    
                    // Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
    
    
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                finally {
                    progressbar.setVisibility(INVISIBLE); // or try View.INVISIBLE
                }
            }
        }
    });
    

    【讨论】:

    • 谢谢,我确实把进度条弄得太复杂了,它的工作原理就像做梦一样。仍然不确定为什么第一个 Toast 不会显示,但我认为您对延迟的看法可能是正确的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多