Android如何显示Toast?

时间:2014-03-04 05:05:21

标签: android button toast

我有这个按钮,当按下时,图像将被保存到外部存储器中。我想用“你的照片已保存”文字显示Toast。

这是我的代码(但是每当我点击保存时它都没有显示吐司):

save.setOnClickListener(new View.OnClickListener() {
    @SuppressLint("ShowToast")
    @SuppressWarnings("deprecation")

    public void onClick(View v) {
        Log.v(TAG, "Save Tab Clicked");

        viewBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(viewBitmap);

        tapimageview.draw(canvas);
        canvas.drawBitmap(bp, 0, 0, paint);
        canvas.drawBitmap(drawingBitmap, matrix, paint);
        canvas.drawBitmap(bmpstickers, matrix, paint);

        //tapimageview.setImageBitmap(mBitmapDrawable.getBitmap());  
        try {
            mBitmapDrawable = new BitmapDrawable(viewBitmap);

            mCurrent = "PXD_" + new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date()) + ".jpg";

            bp1 = mBitmapDrawable.getBitmap();
            tapimageview.setImageBitmap(bp1);
            mNewSaving = ((BitmapDrawable) mBitmapDrawable).getBitmap();

            String FtoSave = mTempDir + mCurrent;
            File mFile = new File(FtoSave);
            mFileOutputStream = new FileOutputStream(mFile);
            mNewSaving.compress(CompressFormat.JPEG, 100, mFileOutputStream);

            mFileOutputStream.flush();
            mFileOutputStream.close();

        } catch (FileNotFoundException e) {
            Log.v(TAG, "FileNotFoundExceptionError " + e.toString());
        } catch (IOException e) {
            Log.v(TAG, "IOExceptionError " + e.toString());
        }

        Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG);
    }
});

4 个答案:

答案 0 :(得分:4)

您忘记了.show()中的Toast,如下所示:

   Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG).show();

您可以使用show()显示Toast通知。

答案 1 :(得分:0)

Toast中的

.show()

 Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

奇怪的是,Toast没有显示错误。 :) 你需要在最后添加show()。

Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG).show();

答案 3 :(得分:0)

只需替换此行

 Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG);

通过这一行

 Toast.makeText(getApplicationContext(), "Your photo has been saved", Toast.LENGTH_LONG).show();