两个布局,一个禁用

时间:2012-04-28 18:27:32

标签: android layout

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

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

  • linlay4只包含4个Spinners,
  • linlay5另外五个Spinners。

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

这与linlay4完美配合,选择了linlay5未设置TextView。

这是我的代码:

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

}

提前致谢! :)

1 个答案:

答案 0 :(得分: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