ImageButton不会更改图像OnClick

时间:2017-07-22 21:21:42

标签: android onclick imagebutton

我是Android Studio的新用户,请原谅我的编码。我想用图像按钮点击启动flash,所以开始点击监听器,我的绘图文件夹中有图像,但图像保持不变:

John111 Due; Джон Дью; was; drawing; the; quick; 123; ۰۱۲; brown; fox; Alex King; draws; a; fox

XML文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fenjer);
    imageButton = (ImageButton) findViewById(R.id.imageButton);
    if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
    {
        camera = camera.open();
        parameters = camera.getParameters();
        isFlash = true;

    }
   imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          if(isFlash)
            {
            if(!isOn)
            {
                imageButton.setImageResource(R.drawable.on);
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                camera.setParameters(parameters);
                camera.startPreview();
                isOn = true;

            }
              else
                {
                    imageButton.setImageResource(R.drawable.off);
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                camera.setParameters(parameters);
                camera.stopPreview();
                isOn = false;

            }
            }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(Fenjer.this);
                        builder.setTitle("Error...");
                builder.setMessage("Flash is not avaiable on this device...");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which){
                        dialog.dismiss();
                        finish();

                }
            });
                AlertDialog alertDialog = builder.create();


            }

        }
    });


}

0 个答案:

没有答案