0

我有Activity一个 RadioGroup,其中包含 2 个 RadioButtons ( rdbtn4, rdbtn5)。

然后我有一个 XML 文件,其中包含LinearLayouts

  • linlay4仅包含 4 个 Spinner,
  • linlay5五个额外的微调器。

然后用户可以根据自己的喜好设置 Spinner,按下按钮后 2 个 TextView 会设置为特定文本(取决于所选的 Spinner 值)。

这与未设置的 TextViews 完美linlay4配合linlay5

这是我的代码:

package at.esdev.electro;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class Widerstandsfarbcode extends Activity
{
    LinearLayout linlay4rings, linlay5rings;
    TextView tvWidResultValue, tvWidToleranzValue;
    Button btnCalcwid;
    Spinner sp4Farbe1, sp4Farbe2, sp4Farbe3, sp4Farbe4, sp5Farbe1, sp5Farbe2, sp5Farbe3, sp5Farbe4, sp5Farbe5;
    RadioGroup rdGrp1;
    RadioButton rdbtn4rings, rdbtn5rings;


public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widerstandsfarbcode);

    linlay4rings = (LinearLayout) findViewById(R.id.linLay4rings);
    linlay5rings = (LinearLayout) findViewById(R.id.linLay5rings);

    tvWidResultValue = (TextView) findViewById(R.id.tvWidResultValue);
    tvWidToleranzValue = (TextView) findViewById(R.id.tvWidToleranzValue);

    btnCalcwid = (Button) findViewById(R.id.btnCalcwid);



    sp4Farbe1 = (Spinner) findViewById(R.id.sp4Farbe1);
    sp4Farbe2 = (Spinner) findViewById(R.id.sp4Farbe2);
    sp4Farbe3 = (Spinner) findViewById(R.id.sp4Farbe3);
    sp4Farbe4 = (Spinner) findViewById(R.id.sp4Farbe4);

    sp5Farbe1 = (Spinner) findViewById(R.id.sp5Farbe1);
    sp5Farbe2 = (Spinner) findViewById(R.id.sp5Farbe2);
    sp5Farbe3 = (Spinner) findViewById(R.id.sp5Farbe3);
    sp5Farbe4 = (Spinner) findViewById(R.id.sp5Farbe4);
    sp5Farbe5 = (Spinner) findViewById(R.id.sp5Farbe5);

    rdGrp1 = (RadioGroup) findViewById(R.id.rdGrp1);

    rdbtn4rings = (RadioButton) findViewById(R.id.rdb4Rings);
    rdbtn5rings = (RadioButton) findViewById(R.id.rdb5Rings);


    //Default-Value when starting Activity:
    linlay4rings.setVisibility(0); //0 = visible, 4 = invisible, 8 = gone
    linlay5rings.setVisibility(8);

    //Layout for 5 buttons disabled, if rdbtn4 is selected:
            rdbtn4rings.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    linlay4rings.setVisibility(0); //0 = visible, 4 = invisible, 8 = gone
                    linlay5rings.setVisibility(8);
                }
            });

            //Layout for 4 buttons disabled, if rdbtn5 is selected:
            rdbtn5rings.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                    linlay4rings.setVisibility(8); //0 = visible, 4 = invisible, 8 = gone
                    linlay5rings.setVisibility(0);
                }
            });

    //Color-adjusting
    ArrayAdapter <?> adapterFarben = ArrayAdapter.createFromResource(this, R.array.widerstandsfarbSpinnerItems, android.R.layout.simple_spinner_item);
    adapterFarben.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp4Farbe1.setAdapter(adapterFarben); 
    sp4Farbe2.setAdapter(adapterFarben);
    sp5Farbe1.setAdapter(adapterFarben);
    sp5Farbe2.setAdapter(adapterFarben);
    sp5Farbe3.setAdapter(adapterFarben);
    //Multiplier-adjusting
    ArrayAdapter <?> adapterMultiplikator = ArrayAdapter.createFromResource(this, R.array.widerstandsMultiplikatorSpinnerItems, android.R.layout.simple_spinner_item);
    adapterMultiplikator.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp4Farbe3.setAdapter(adapterMultiplikator);
    sp5Farbe4.setAdapter(adapterMultiplikator);
    //Tolerance-adjusting:
    ArrayAdapter <?> adapterToleranz = ArrayAdapter.createFromResource(this, R.array.widerstandsToleranzSpinnerItems, android.R.layout.simple_spinner_item);
    adapterFarben.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp4Farbe4.setAdapter(adapterToleranz);
    sp5Farbe5.setAdapter(adapterToleranz);


    if (rdbtn4rings.isChecked())
    {
        btnCalcwid.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                String s41 = String.valueOf(sp4Farbe1.getSelectedItemId()); //Farbe 1
                String s42 = String.valueOf(sp4Farbe2.getSelectedItemId()); //Farbe 2
                String s43 = String.valueOf(sp4Farbe3.getSelectedItemId()); //Multiplikator
                String s44 = String.valueOf(sp4Farbe4.getSelectedItemId()); //Toleranz
                int ds43 = Integer.parseInt(s43);
                int ds44 = Integer.parseInt(s44);
                rise1(ds43); 
                String snewTolerance = String.valueOf(getToleranceValue(ds44));

                //Output:

                tvWidResultValue.setText("" + s41 + s42 + "*10^" + ds43 + " Ohm");
                tvWidToleranzValue.setText(""+ snewTolerance + " %");
            }
        }); 
    }

    if (rdbtn5rings.isChecked())
    {
        btnCalcwid.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                String s51 = String.valueOf(sp5Farbe1.getSelectedItemId()); //Farbe 1
                String s52 = String.valueOf(sp5Farbe2.getSelectedItemId()); //Farbe 2
                String s53 = String.valueOf(sp5Farbe3.getSelectedItemId()); //Farbe 3
                String s54 = String.valueOf(sp5Farbe4.getSelectedItemId()); //Multiplikator
                String s55 = String.valueOf(sp5Farbe5.getSelectedItemId()); //Toleranz
                int ds54 = Integer.parseInt(s54);
                int ds55 = Integer.parseInt(s55);
                rise1(ds54);
                String snewTolerance = String.valueOf(getToleranceValue(ds55));

                //Output:

                tvWidResultValue.setText("" + s51 + s52 + s53 + "*10^" + ds54 + " Ohm");
                tvWidToleranzValue.setText(""+ snewTolerance + " %");
            }//onClick
        });
    }
}


public double rise1(double multiplikValue)
{
    if (multiplikValue > -1)    
    {
        if (multiplikValue < 7) 
        {
            multiplikValue = multiplikValue++;
        }
    }
    if (multiplikValue == 7)
    {
        multiplikValue = -1; //weil 10^-1 = 0.1
    }
    if (multiplikValue == 8)
    {
        multiplikValue = -2; //weil 10^-2 = 0.01
    }
    return multiplikValue;
}

public double getToleranceValue(double toleranz)
{
    double newTolerance = 0; 
    if (toleranz == 0) //Spinnerposition
    {
        newTolerance = 1; //Toleranz-%-Wert
    }
    if (toleranz == 1 )
    {
        newTolerance = 2;
    }
    if (toleranz == 2 )
    {
        newTolerance = 0.5;
    }
    if (toleranz == 3 )
    {
        newTolerance = 0.25;
    }
    if (toleranz == 4 )
    {
        newTolerance = 0.1;
    }
    if (toleranz == 5 )
    {
        newTolerance = 5;
    }
    if (toleranz == 6 )
    {
        newTolerance = 10;
    }
    return newTolerance;
}

}

提前致谢!:)

4

1 回答 1

0

你的变量名非常神秘,但我想我明白了什么是错的。尝试更改 btnCalcWid 中的检查,如下所示:

btnCalcwid.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        if (rdbtn4rings.isChecked())
        {
            String s41 = String.valueOf(sp4Farbe1.getSelectedItemId()); //Farbe 1
            String s42 = String.valueOf(sp4Farbe2.getSelectedItemId()); //Farbe 2
            String s43 = String.valueOf(sp4Farbe3.getSelectedItemId()); //Multiplikator
            String s44 = String.valueOf(sp4Farbe4.getSelectedItemId()); //Toleranz
            int ds43 = Integer.parseInt(s43);
            int ds44 = Integer.parseInt(s44);
            rise1(ds43); 
            String snewTolerance = String.valueOf(getToleranceValue(ds44));

            //Output:

            tvWidResultValue.setText("" + s41 + s42 + "*10^" + ds43 + " Ohm");
            tvWidToleranzValue.setText(""+ snewTolerance + " %");
        }
        else if (rdbtn5rings.isChecked())
        {
            String s51 = String.valueOf(sp5Farbe1.getSelectedItemId()); //Farbe 1
            String s52 = String.valueOf(sp5Farbe2.getSelectedItemId()); //Farbe 2
            String s53 = String.valueOf(sp5Farbe3.getSelectedItemId()); //Farbe 3
            String s54 = String.valueOf(sp5Farbe4.getSelectedItemId()); //Multiplikator
            String s55 = String.valueOf(sp5Farbe5.getSelectedItemId()); //Toleranz
            int ds54 = Integer.parseInt(s54);
            int ds55 = Integer.parseInt(s55);
            rise1(ds54);
            String snewTolerance = String.valueOf(getToleranceValue(ds55));

            //Output:

            tvWidResultValue.setText("" + s51 + s52 + s53 + "*10^" + ds54 + " Ohm");
            tvWidToleranzValue.setText(""+ snewTolerance + " %");
        }//else if
    }//onClick
});//setOnClickListener
于 2012-04-30T21:41:26.767 回答