我是新写的。我有一个带有组的菜单,在我所在的组中,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()
,知道在菜单中选择了女巫选项以调用正确的布局。
如何检查菜单中选择的女巫选项?我很抱歉我的英语。