Android - 如何隐藏/显示进度条 onclick

时间:2022-12-01
本文介绍了Android - 如何隐藏/显示进度条 onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时显示进度条.当我测试应用程序时,它会强制关闭/停止.在添加进度条代码之前,我的应用程序运行良好.

I am trying to show a progressbar when a button is clicked. When i test the app it force closes / stops. My app works fine before the progressbar code is added in.

如果这很重要,我也使用切换按钮,我希望旋转进度条不是对话框.

also i am using a toggle button if that can matters, and i want the spinning progressbar not a dialog.

有什么帮助吗?

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.buttonFlashlight);
    Context context = this;
    PackageManager pm = context.getPackageManager();


    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Log.e("err", "Device has no camera!");
        return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            if (isLighOn) {

                progressBar1.setVisibility(View.INVISIBLE);

                final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);  
                mp1.start(); 

                Log.i("info", "torch is turn off!");
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                isLighOn = false;

                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.screenBrightness = 1.0f;
                getWindow().setAttributes(params);

            } else {

                progressBar1.setVisibility(View.VISIBLE);

                final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);  
                mp1.start(); 

                Log.i("info", "torch is turn on!");
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                isLighOn = true;

                WindowManager.LayoutParams params = getWindow().getAttributes();
                params.screenBrightness = 0.05f;
                getWindow().setAttributes(params);
            }
        }
    });
    }

推荐答案

试试下面的代码...

handler1 = new Handler()
{
            @Override
            public void handleMessage(Message msg) 
            {
                switch(msg.what)
                {
                case 1:
                    static_class.digi_pd = ProgressDialog.show(Create_Digitizing_Job.this, "Loading...", "Please Wait..", true,false);
                    static_class.digi_pd.setCancelable(false);
                    break;
                case 2:
                    static_class.digi_pd.dismiss();
                    break;

                }
            }
        };

        thread1 = new Thread()
          {
                @Override
                public void run()
                {   
                       try
                        {  


                          handler1.sendEmptyMessage(1);

                    // write your code here....

                         handler1.sendEmptyMessage(3);
                         handler1.sendEmptyMessage(2);

                        } 
                       catch (Exception e) 
                       {
                           Log.w("thread error...",""+e);
                           //e.printStackTrace();
                       }   

             }

         };
        thread1.start();

这篇关于Android - 如何隐藏/显示进度条 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:选择的图像未在 mainActivity 中显示 下一篇:什么是“ECLIPSE SDK"?

相关文章

最新文章