0

我是新写的。我有一个带有组的菜单,在我所在的组中,android:checkableBehavior="single"有两个选项可以使主布局内的包含布局可见或不可见。

public class Main extends Activity {

private String settingSet = "kgcm";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

  //Creo el spinner de sexo
    final Spinner spinner1=(Spinner)findViewById(R.id.spinnerSexo);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.spinnerSexo, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);

    //Creo el spinner de edad
    Spinner spinner2=(Spinner)findViewById(R.id.spinnerEdad);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.spinnerEdad, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);  

    //Configuracion del boton
    Button bCalcular = (Button)findViewById(R.id.bcalcular);
    bCalcular.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String sexo = spinner1.getSelectedItem().toString();
            String imc1 = formulaDatos(spinner1);

            if (sexo.equals("Hombre"))
            {
                //Ficha de Hombre
                colocarIMCH(imc1);
                rellenarHombre(imc1);
            }
            else{
                //Ficha de Mujer
                colocarIMCM(imc1);
                rellenarMujer(imc1);
            }

        }
    });


    }

protected void rellenarMujer(String imc1) {
    if (imc1.compareTo("18.50") < 0){
        bajoPesoM();

    }
    else{
        if(imc1.compareTo("24.90") < 0){
            normalM();
        }
        else{
            if(imc1.compareTo("29.90") < 0){
                sobrePesoM();
            }
            else{
                if(imc1.compareTo("34.90") < 0){
                    obesidad1M();
                }
                else{
                    if(imc1.compareTo("39.9") < 0){
                        obesidad2M();
                    }
                    else{
                        obesidad3M();
                        }
                }
            }
        }
    }

}

protected void rellenarHombre(String imc1) {
    if (imc1.compareTo("18.50") < 0){
        bajoPeso();

    }
    else{
        if(imc1.compareTo("24.90") < 0){
            normal();
        }
        else{
            if(imc1.compareTo("29.90") < 0){
                sobrePeso();
            }
            else{
                if(imc1.compareTo("34.90") < 0){
                    obesidad1();
                }
                else{
                    if(imc1.compareTo("39.9") < 0){
                        obesidad2();
                    }
                    else{
                        obesidad3();
                        }
                }
            }
        }
    }

}

protected String formulaDatos(Spinner spinner1) {
    //configurando los editext para obtener el string
    EditText editTextAltura = (EditText)findViewById(R.id.editTextAltura);
    EditText editTextPeso = (EditText)findViewById(R.id.editTextPeso);

    float altura = Integer.parseInt(editTextAltura.getText().toString());
    altura = altura / 100;
    float peso = Integer.parseInt(editTextPeso.getText().toString());


    float imc = (peso / (altura * altura));
    String imc1 = String.format("%.2f", imc);
    return imc1;
}

protected void bajoPeso() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.bajoPesoH);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);

    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.bajoPesoHD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void bajoPesoM() {
    //Colocar un consejo Aleatorio en Hombre TextView4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.bajoP);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);

            //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.bajoPD);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void normal() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.normalH);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);
    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.normalHD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}
protected void normalM() {
    //Colocar un consejo Aleatorio en MUJER TextView4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.normal);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);
            //Colocar un consejo Aleatorio deportuvo en TextView6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.normalD);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void sobrePeso() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.sobreH);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);
    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.sobreHD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void sobrePesoM() {
    //Colocar un consejo Aleatorio en MUJER TextViewM4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.sobreP);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);
            //Colocar un consejo Aleatorio deportuvo en TextViewM6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.sobrePD);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad1() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.ove1H);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);
    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.ove1HD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad1M() {
    //Colocar un consejo Aleatorio en MUJER TextViewM4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.ove1);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);
            //Colocar un consejo Aleatorio deportuvo en TextViewM6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.ove1D);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad2() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.ove2H);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);
    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.ove2HD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad2M() {
    //Colocar un consejo Aleatorio en MUJER TextView4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.ove2);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);
            //Colocar un consejo Aleatorio deportuvo en TextView6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.ove2D);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad3() {
    //Colocar un consejo Aleatorio en Hombre TextView4
    TextView tv4 = (TextView)findViewById(R.id.textViewH4);
    String[] bph = (String[])getResources().getStringArray(R.array.ove3H);
    int max = bph.length;
    tv4.setText(bph[getRandomNumber(max)]);
    //Colocar un consejo Aleatorio deportuvo en Hombre TextView6
    TextView tv6 = (TextView)findViewById(R.id.textViewH6);
    String[] dph = (String[])getResources().getStringArray(R.array.ove3HD);
    int maxd = dph.length;
    tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void obesidad3M() {
    //Colocar un consejo Aleatorio en MUJER TextViewM4
            TextView tv4 = (TextView)findViewById(R.id.textViewM4);
            String[] bph = (String[])getResources().getStringArray(R.array.ove3);
            int max = bph.length;
            tv4.setText(bph[getRandomNumber(max)]);
            //Colocar un consejo Aleatorio deportuvo en TextViewM6
            TextView tv6 = (TextView)findViewById(R.id.textViewM6);
            String[] dph = (String[])getResources().getStringArray(R.array.ove3D);
            int maxd = dph.length;
            tv6.setText(dph[getRandomNumber(maxd)]);

}

protected void colocarIMCH(String imc1) {

    //cargar layout que vamos a usar
    setContentView(R.layout.layout_hombre);
    //Colacamos el IMC en el textView
    TextView textViewH2 = (TextView)findViewById(R.id.textViewH2);
    textViewH2.setText(imc1);

}


protected void colocarIMCM(String imc1){

    //Cargar layout que vamos a usar
    setContentView(R.layout.layout_mujer);
    //Colacamos el IMC en el textView
    TextView textViewM2 = (TextView)findViewById(R.id.textViewM2);
    textViewM2.setText(imc1);
}


protected int getRandomNumber(int max){

    int number = (new Random().nextInt(max));
    return number;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    View include = findViewById(R.id.includeinch);

    switch (item.getItemId()){
    case R.id.kgcm:
        if (item.isChecked());
        else{
            item.setChecked(true);
            settingSet = "kgcm";
            if(include != null){
            findViewById(R.id.includeinch).setVisibility(View.INVISIBLE);
            findViewById(R.id.includekg).setVisibility(View.VISIBLE);
            }
        }
        return true;
    case R.id.inch:
        if (item.isChecked());
        else{
            item.setChecked(true);
            settingSet = "inch";
            if(include != null){
            findViewById(R.id.includekg).setVisibility(View.INVISIBLE);
            findViewById(R.id.includeinch).setVisibility(View.VISIBLE);
            }
        }
        return true;

    case R.id.salir:
        salir();
        return true;
    case R.id.inicio:
        inici();
        return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}

protected void salir() {
    finish();

}

private void inici() {

    setContentView(R.layout.layout_main);

    if(settingSet.equals("inch")){
        findViewById(R.id.includekg).setVisibility(View.INVISIBLE);
        findViewById(R.id.includeinch).setVisibility(View.VISIBLE);
    }
    else{
        findViewById(R.id.includekg).setVisibility(View.VISIBLE);
        findViewById(R.id.includeinch).setVisibility(View.INVISIBLE);
    }
}

}

我知道我不能这样做:

MenuItem menu = (MenuItem)findViewById(R.id.inch); MenuItem menu2 = (MenuItem)findViewById(R.id.kgcm);

当我使用菜单选项更改某些内容并希望返回主菜单时inici(),知道在菜单中选择了女巫选项以调用正确的布局。

如何检查菜单中选择的女巫选项?我很抱歉我的英语。

4

1 回答 1

0

我无法理解这一点:

当我回到主目录时,如果我更改大小写 R.id.inch,此选项不会更改为默认值,但我的包含恢复为默认值,然后它就无法正常工作。

但我注意到在return trueeach 的末尾使用了case,将其替换为break
并重试。

问题是您正在访问不存在的视图,因为它们已通过setContentView
一个有效的示例进行了更改:

public class MainActivity extends Activity {

    private String SettingSet;//Save what choice is set. Will be used in inici()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    public void onClickHombre(View v){

        setContentView(R.layout.hombre_layout);
    }

    public void onClickMujer(View v){

        setContentView(R.layout.mujer_layout);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        View include = findViewById(R.id.includeinch);

        switch (item.getItemId()){
        case R.id.kgcm:
            if (item.isChecked());
            else{
                item.setChecked(true);
                SettingSet = "kgcm";
                if(include != null){//Do that only if in Main
                    findViewById(R.id.includeinch).setVisibility(View.INVISIBLE);
                    findViewById(R.id.includekg).setVisibility(View.VISIBLE);
                }
            }
            return true;
        case R.id.inch:
            if (item.isChecked());
            else{
                item.setChecked(true);
                SettingSet = "inch";
                if(include != null){//Do that only if in Main
                    findViewById(R.id.includekg).setVisibility(View.INVISIBLE);
                    findViewById(R.id.includeinch).setVisibility(View.VISIBLE);
                }   
            }
            return true;

        case R.id.salir:
            salir();
            return true;
        case R.id.inicio:
                //this function back on to main layout
            inici();
            return true;
        default:
        return super.onOptionsItemSelected(item);
        }
    }

    private void inici() {
        setContentView(R.layout.activity_main);
        if(SettingSet == "inch"){
            findViewById(R.id.includekg).setVisibility(View.INVISIBLE);
            findViewById(R.id.includeinch).setVisibility(View.VISIBLE);
        }
        else{
            findViewById(R.id.includekg).setVisibility(View.VISIBLE);
            findViewById(R.id.includeinch).setVisibility(View.INVISIBLE);
        }
    }

    private void salir() {
        // TODO Auto-generated method stub
    }
}  

无论如何,最好为 Hombre 使用一个活动,为 Mujer 使用另一个活动

有办法解决这个问题,但我再说一遍,这不是一个好习惯。

1 - 将所有代码从onCreate移至inici(). 打电话inici()onCreate
2 -inici()为每个实现 OnItemSelectedListenerSpinner并将所选项目保存到字段中。
3 - 在inici()恢复从该字段中选择的项目。
4 - 在findViewById使用 Hombre 或 Mujer 中的任何视图调用之前setContentView,使用相应的布局(Hombre 或 Mujer)调用。在使用 Hombre 或 Mujer 呼叫完成所有工作之后inici()(屏幕可能会闪烁)。
这可以解决,但我相信其他问题会来。

于 2013-07-27T16:26:42.287 回答