更改两个Button成切换按钮 [英] Changing two button into a toggle button

查看:214
本文介绍了更改两个Button成切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用两个按钮(on_btn和off_btn)打开和关闭手电筒手电筒code。

我怎样才能在一个按键他们交往?

新手很可能你给精心建议好吗?

下code是从答案中发现:它适用于我的手机。但在模拟器上它,而在按钮上点击crashs。这里是猫登录 https://dl.dropbox.com/u/15065300/logcat1巴纽

行号74是:参数PARAMS = mCamera.getParameters();

有谁能够什么建议吗?

 公共类手电筒延伸活动{
    私人最终静态字符串LOG_TAG =手电筒
    私人按钮mOnBtn;
    私人相机mCamera;
    私人布尔isActive;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);      //这将是你的onCreate内...
        mOnBtn =(按钮)findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(新OnClickListener(){            公共无效的onClick(视图v){
                flipSwitch();
                processClick();            }
        });
   }    //这将是你外面的onCreate
    公共无效flipSwitch(){
        isActive = isActive!;
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        尝试{
            mCamera = Camera.open();
            mCamera.start preVIEW();
            Toast.makeText(getApplicationContext()相机是present,Toast.LENGTH_LONG).show();        }赶上(例外五){
            Log.e(LOG_TAG,不可能D'ouvrir LA相机);
        }
    }    @覆盖
    保护无效的onPause(){
        如果(mCamera!= NULL){
            mCamera.release();
            mCamera = NULL;
        }
        super.onPause();
    }    公共无效processClick(){
        如果(isActive){
            参数PARAMS = mCamera.getParameters();
            params.set(闪光模式,火炬);
            mCamera.setParameters(PARAMS);
            mCamera.start preVIEW();
        }        其他{             参数PARAMS = mCamera.getParameters();
             params.set(闪光模式,关);
             mCamera.setParameters(PARAMS);
             mCamera.stop preVIEW();        }
    }
}


解决方案

使用按钮翻转一个布尔值,并改变你的手电筒去开启和关闭相应。

 私人布尔isActive;//这将是你的onCreate内...
button.setOnClickListener(新View.onClickListener(){
    公共无效的onClick(视图v){
        flipSwitch();
        processClick();
    }
}//这将是你外面的onCreate
公共无效flipSwitch(){
    isActive = isActive!;
}公共无效processClick(){
    如果(isActive){
        //按钮被点击
    }
    其他{
        //按钮被点击关闭
    }
}

I have a Flashlight code that use two buttons (on_btn and off_btn) to turn on and turn off the Flashlight.

How can I associate them in a single button?

Very novice can you give elaborate suggestion please?

The code below is found from an answer: And it works for my mobile. But on the emulator it crashs while clicking on the button. Here is the cat-log https://dl.dropbox.com/u/15065300/logcat1.png

The line number 74 is: Parameters params = mCamera.getParameters();

Can anybody any suggestion please?

public class FlashLight extends Activity {


    private final static String LOG_TAG = "FlashLight";  
    private Button mOnBtn;   
    private Camera mCamera;    
    private boolean isActive;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      //this will be inside your onCreate...
        mOnBtn = (Button) findViewById(R.id.on_btn);
        mOnBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                flipSwitch();
                processClick();

            }
        });
   }

    //these will be outside your onCreate
    public void flipSwitch() {
        isActive = !isActive;
    }

    @Override
    protected void onResume() {
        super.onResume();
        try{
            mCamera = Camera.open();
            mCamera.startPreview();
            Toast.makeText(getApplicationContext(),"Camera is  present", Toast.LENGTH_LONG).show();

        } catch( Exception e ){
            Log.e(LOG_TAG, "Impossible d'ouvrir la camera");
        }
    }

    @Override
    protected void onPause() {
        if( mCamera != null ){
            mCamera.release();
            mCamera = null;
        }
        super.onPause();
    }

    public void processClick() {
        if(isActive) {
            Parameters params = mCamera.getParameters();
            params.set("flash-mode", "torch");
            mCamera.setParameters( params );
            mCamera.startPreview();
        }

        else { 

             Parameters params = mCamera.getParameters();
             params.set("flash-mode", "off");
             mCamera.setParameters( params );
             mCamera.stopPreview();

        }
    }
}

解决方案

use the button to flip a boolean value, and change your flashlight to go on and off accordingly.

private boolean isActive;

//this will be inside your onCreate...
button.setOnClickListener(new View.onClickListener() {
    public void onClick(View v) {
        flipSwitch();
        processClick();
    }
}

//these will be outside your onCreate
public void flipSwitch() {
    isActive = !isActive;
}

public void processClick() {
    if(isActive) {
        //button is clicked on
    }
    else {
        //button is clicked off
    }
}

这篇关于更改两个Button成切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆