Java 开关按钮实现

Java 开关按钮实现,java,android,user-interface,Java,Android,User Interface,我正在使用开关按钮将值从一个单位转换为另一个单位 也就是说,我将为计算输入所有必要的参数。从开关按钮中选择选项后,它将根据用户在我通过单击计算按钮启动计算后选择的单位进行输出 然而,我目前面临的问题是,即使没有点击“计算”按钮,我也可以使用“切换”按钮执行计算 我知道我的问题在于以下几行代码 double result = 0; if (v == calButton) { double lamda = 300000000 / freq;

我正在使用开关按钮将值从一个单位转换为另一个单位

也就是说,我将为计算输入所有必要的参数。从开关按钮中选择选项后,它将根据用户在我通过单击计算按钮启动计算后选择的单位进行输出

然而,我目前面临的问题是,即使没有点击“计算”按钮,我也可以使用“切换”按钮执行计算

我知道我的问题在于以下几行代码

double result = 0;
        if (v == calButton) {
            double lamda = 300000000 / freq;

            result = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
            //double radius = 5000;
            ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(result));

            final double finalResult = result;
            Switch select_unit=(Switch)llLayout.findViewById(R.id.switch1);
            select_unit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(finalResult));
                    } else {
                        ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(Double.toString(10 * Math.log10(finalResult * 1000)));
                    }
                }
            });
从上面的代码来看,问题显然是因为我用相同的方法实现了计算和切换按钮。我试图将OnCheckChanged方法从中分离出来。然而,我们遇到了错误。例如,它声明从不使用变量结果

有人能提供一些指导/提示吗

这是我的界面:

我的完整代码:


您已经在onClickView v方法中填充了所有内容。你确定吗?无论您在屏幕上点击何处,每次都会触发此事件。除非你需要,否则不要做。在onCreateView方法中,根据您的要求分别为每个控件执行此操作。您已经在calculate按钮的实现中实现了switch按钮的接口。将它们分开,并将开关按钮的状态保存在某个全局变量中。访问“计算”按钮侦听器中的全局变量。希望能有帮助!!最好的

尝试这样更改代码:

public class FreeSpaceActivity extends Fragment implements OnClickListener {

    EditText tPowerEdit = null;
    EditText tGainEdit = null;
    EditText rGainEdit = null;
    EditText freqEdit = null;
    EditText distanceEdit = null;
    EditText lightEdit = null;
    Button calButton = null;
    Button graphButton = null;
    Button clearButton = null;
    Button plotButton = null;
    Switch switchButton=null;

    EditText radEdit = null;//testing

    private ScrollView llLayout;
    private FragmentActivity faActivity;
    //private Switch switch1;
    // Changes here
    private double mResult = 0;
    private Switch mSelectUnit;
    private TextView mTextFreeSpaces;
    private boolean mIsCalculated;// flag to show the result only when you click on calculate button


    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        faActivity = (FragmentActivity) this.getActivity();
        llLayout = (ScrollView) inflater.inflate(R.layout.activity_free_space, container, false);

        tPowerEdit = (EditText)llLayout.findViewById(R.id.editText1);
        tGainEdit = (EditText)llLayout.findViewById(R.id.editText2);
        rGainEdit = (EditText)llLayout.findViewById(R.id.editText3);
        freqEdit = (EditText)llLayout.findViewById(R.id.editText4);
        distanceEdit = (EditText)llLayout.findViewById(R.id.editText7);
        lightEdit = (EditText)llLayout.findViewById(R.id.editText8);
        calButton = (Button)llLayout.findViewById(R.id.button1);
        calButton.setOnClickListener(this);
        graphButton = (Button)llLayout.findViewById(R.id.button2);
        graphButton.setOnClickListener(this);
        clearButton = (Button)llLayout.findViewById(R.id.button3);
        clearButton.setOnClickListener(this);
        plotButton =(Button)llLayout.findViewById(R.id.plot_button);
        plotButton.setOnClickListener(this);

        radEdit = (EditText)llLayout.findViewById(R.id.editText28);//testing

        // Changes here
        mTextFreeSpaces = (TextView) llLayout.findViewById(R.id.textFreeSpaces)

        mSelectUnit =(Switch)llLayout.findViewById(R.id.switch1);
        mSelectUnit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    updateFreeSpacesText(isChecked);
                }
            });
        return llLayout;
    }

    /*private ActionBar getSupportActionBar() {
        return null;
    }*/

    private void updateFreeSpacesText(boolean isSwitcherChecked) {
        if(mIsCalculated){
           if(isSwitcherChecked){
                mTextFreeSpaces.setText(Double.toString(mResult));
           } else {
                mTextFreeSpaces.setText(Double.toString(10 * Math.log10(mResult* 1000)));
           }
        }

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String tPowerStr = tPowerEdit.getText().toString();
        String tGainStr = tGainEdit.getText().toString();
        String rGainStr = rGainEdit.getText().toString();
        String freqStr = freqEdit.getText().toString();
        String distanceStr = distanceEdit.getText().toString();
        String lightStr = lightEdit.getText().toString();
        double tPower = Double.parseDouble(!tPowerStr.isEmpty() ? tPowerStr : "0");
        double tGain = Double.parseDouble(!tGainStr.isEmpty() ? tGainStr : "0");
        double rGain = Double.parseDouble(!rGainStr.isEmpty() ? rGainStr : "0");
        double freq = Double.parseDouble(!freqStr.isEmpty() ? freqStr : "0");
        double distance = Double.parseDouble(!distanceStr.isEmpty() ? distanceStr : "0");
        double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "0");

        String radStr = radEdit.getText().toString();//testing
        double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing


        if (v == calButton) {
            double lamda = 300000000 / freq;

            mResult = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
          mIsCalculated = true;
          updateFreeSpacesText(mSelectUnit.isChecked());


        }
        else if (v == graphButton) {

            Intent intent = new Intent(getActivity(), GraphActivity.class);
            intent.putExtra("model", "freeSpace");
            intent.putExtra("tp", tPower);
            intent.putExtra("tg", tGain);
            intent.putExtra("rg", rGain);
            intent.putExtra("f", freq);
            intent.putExtra("l", light);
            this.startActivity(intent);
        } else if (v == clearButton) {
            ((EditText) llLayout.findViewById(R.id.editText1)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText2)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText3)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText4)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText7)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText8)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText28)).setText("");
            ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText("");
            //((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText("");
        } else if (v == plotButton) {
            //double radius = Double.parseDouble(radius.getText().toString());
            Intent intent = new Intent(getActivity(), MapActivity.class);
            Bundle b = new Bundle();
            b.putDouble("radius", result);
            intent.putExtras(b);
            this.startActivity(intent);
            }
        }



}

嗯,我有一个问题,如果我要切换按钮。例如,默认单位是W,我想切换到dbm,它会自动输出答案,而无需单击“计算”按钮。。。在单击“计算”按钮之前,我添加了一个标志来阻止更新TextView。您是否尝试过我编辑的答案?否则,如果您只想在单击“计算”按钮时更新TextView,您可以从切换器中删除setOnCheckedChangeListener。嗯?我可以知道你做了哪些改变吗?我现在看不到任何差异>测试。对不起:-
public class FreeSpaceActivity extends Fragment implements OnClickListener {

    EditText tPowerEdit = null;
    EditText tGainEdit = null;
    EditText rGainEdit = null;
    EditText freqEdit = null;
    EditText distanceEdit = null;
    EditText lightEdit = null;
    Button calButton = null;
    Button graphButton = null;
    Button clearButton = null;
    Button plotButton = null;
    Switch switchButton=null;

    EditText radEdit = null;//testing

    private ScrollView llLayout;
    private FragmentActivity faActivity;
    //private Switch switch1;
    // Changes here
    private double mResult = 0;
    private Switch mSelectUnit;
    private TextView mTextFreeSpaces;
    private boolean mIsCalculated;// flag to show the result only when you click on calculate button


    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        faActivity = (FragmentActivity) this.getActivity();
        llLayout = (ScrollView) inflater.inflate(R.layout.activity_free_space, container, false);

        tPowerEdit = (EditText)llLayout.findViewById(R.id.editText1);
        tGainEdit = (EditText)llLayout.findViewById(R.id.editText2);
        rGainEdit = (EditText)llLayout.findViewById(R.id.editText3);
        freqEdit = (EditText)llLayout.findViewById(R.id.editText4);
        distanceEdit = (EditText)llLayout.findViewById(R.id.editText7);
        lightEdit = (EditText)llLayout.findViewById(R.id.editText8);
        calButton = (Button)llLayout.findViewById(R.id.button1);
        calButton.setOnClickListener(this);
        graphButton = (Button)llLayout.findViewById(R.id.button2);
        graphButton.setOnClickListener(this);
        clearButton = (Button)llLayout.findViewById(R.id.button3);
        clearButton.setOnClickListener(this);
        plotButton =(Button)llLayout.findViewById(R.id.plot_button);
        plotButton.setOnClickListener(this);

        radEdit = (EditText)llLayout.findViewById(R.id.editText28);//testing

        // Changes here
        mTextFreeSpaces = (TextView) llLayout.findViewById(R.id.textFreeSpaces)

        mSelectUnit =(Switch)llLayout.findViewById(R.id.switch1);
        mSelectUnit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    updateFreeSpacesText(isChecked);
                }
            });
        return llLayout;
    }

    /*private ActionBar getSupportActionBar() {
        return null;
    }*/

    private void updateFreeSpacesText(boolean isSwitcherChecked) {
        if(mIsCalculated){
           if(isSwitcherChecked){
                mTextFreeSpaces.setText(Double.toString(mResult));
           } else {
                mTextFreeSpaces.setText(Double.toString(10 * Math.log10(mResult* 1000)));
           }
        }

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String tPowerStr = tPowerEdit.getText().toString();
        String tGainStr = tGainEdit.getText().toString();
        String rGainStr = rGainEdit.getText().toString();
        String freqStr = freqEdit.getText().toString();
        String distanceStr = distanceEdit.getText().toString();
        String lightStr = lightEdit.getText().toString();
        double tPower = Double.parseDouble(!tPowerStr.isEmpty() ? tPowerStr : "0");
        double tGain = Double.parseDouble(!tGainStr.isEmpty() ? tGainStr : "0");
        double rGain = Double.parseDouble(!rGainStr.isEmpty() ? rGainStr : "0");
        double freq = Double.parseDouble(!freqStr.isEmpty() ? freqStr : "0");
        double distance = Double.parseDouble(!distanceStr.isEmpty() ? distanceStr : "0");
        double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "0");

        String radStr = radEdit.getText().toString();//testing
        double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing


        if (v == calButton) {
            double lamda = 300000000 / freq;

            mResult = tPower * tGain * rGain * Math.pow(lamda / (4 * Math.PI * distance), 2) / light / 100;
          mIsCalculated = true;
          updateFreeSpacesText(mSelectUnit.isChecked());


        }
        else if (v == graphButton) {

            Intent intent = new Intent(getActivity(), GraphActivity.class);
            intent.putExtra("model", "freeSpace");
            intent.putExtra("tp", tPower);
            intent.putExtra("tg", tGain);
            intent.putExtra("rg", rGain);
            intent.putExtra("f", freq);
            intent.putExtra("l", light);
            this.startActivity(intent);
        } else if (v == clearButton) {
            ((EditText) llLayout.findViewById(R.id.editText1)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText2)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText3)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText4)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText7)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText8)).setText("");
            ((EditText) llLayout.findViewById(R.id.editText28)).setText("");
            ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText("");
            //((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText("");
        } else if (v == plotButton) {
            //double radius = Double.parseDouble(radius.getText().toString());
            Intent intent = new Intent(getActivity(), MapActivity.class);
            Bundle b = new Bundle();
            b.putDouble("radius", result);
            intent.putExtras(b);
            this.startActivity(intent);
            }
        }



}