简体   繁体   English

开关按钮实现

[英]Switch Button Implementation

I'm working on converting the value from an unit conversion to another using switch button. 我正在使用切换按钮将值从单位转换转换为另一个。

Meaning to say, I will input all necessary parameters for the calculation. 意思是说,我将输入所有必要的参数进行计算。 Upon choosing the option from the switch button, it will output according to unit chosen by user after I initiated the calculation by clicking on the "Calculate" button only. 从切换按钮中选择选项后,仅通过单击“计算”按钮启动计算后,它将根据用户选择的单位输出。

However, the current problem I'm facing is even without clicking on the "Calculate" button, I was able to perform the calculation using the switch button too. 但是,我目前面临的问题是即使没有单击“计算”按钮,我也能够使用切换按钮来执行计算。

I know that my problem shall lies on the following lines of code. 我知道我的问题应该出在以下几行代码上。

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)));
                    }
                }
            });

From the code above, obviously the problem came because I implemented both the "Calculate" & "Switch" button in the same method. 从上面的代码中,显然出现了问题,因为我用同一方法实现了“计算”和“切换”按钮。 I tried to separate the OnCheckChanged method out of it. 我试图从中分离出OnCheckChanged方法。 However was greeted with errors. 但是遇到了错误。 As example, it states that the variable result is never used. 例如,它声明从未使用变量结果。

Can someone provide some guides/hints? 有人可以提供一些指南/提示吗?

This is my interface: 这是我的界面:

在此处输入图片说明

My full code: 我的完整代码:

package com.epsilon.rfcalculator.activity;

import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;

import com.epsilon.rfcalculator.R;

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;

    //@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



        return llLayout;
    }

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

    @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


        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)));
                    }
                }
            });



        }
        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);
            }
        }



}

You've stuffed everything inside the onClick(View v) method. 您已将所有内容塞入onClick(View v)方法中。 Are you sure about it? 你确定吗? This event will be fired every time no matter where you tap on the screen. 无论您在屏幕上的什么位置,每次都会触发此事件。 Don't do it unless you require it. 除非需要,否则不要这样做。 Do it individually for each control according to your requirement within the onCreateView method. 根据您在onCreateView方法中的要求,对每个控件分别进行操作。 You've implemented interface for switch button within the implementation of calculate button. 您已经在计算按钮的实现中实现了开关按钮的接口。 Separate them and save the state of the switch button in some global variable. 将它们分开,然后将开关按钮的状态保存在某些全局变量中。 Access the global variable within the calculate button listener. 访问calculate按钮侦听器中的全局变量。 Hope it helps!! 希望能帮助到你!! All the best 祝一切顺利

Try to change your code like that: 尝试像这样更改代码:

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);
            }
        }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM