Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

I can not create buttons for possible answers android studio

I have an issue with a small project in android studio where on animal drawings guess things etc. But guess I have to write the name to accept a button and image lights up and goes to the next , what I want you to show me an answer with buttons say 3 button 1 is the correct answer and the other two false I took a long time with this and even I can not do it if I would appreciate any help

Here the code of the class where the shadows and images run

public class Categoria extends Activity {

    public static String[] nombre_cosa={"cerdo","ave","caballo","conejo","elefante","gallina","gato",
            "rana","perro","pato","oveja","leon","jirafa",
            "raton","vaca","autobus","automovil","avion","bicicleta","camioneta",
            "casa","celular","guitarra","motocicleta","silla","television","durazno","fresa","mango",
            "uvas","sandia","platano","coco","pera","naranja","manzana",
            "bart","batman","cerebro","chavo","goku","homero","marge",
            "patricio","pepa","phineas","quico","spiderman","thor","superman"};

    public static String[] sombra_cosa={"s_cerdo","s_ave","s_caballo","s_conejo","s_elefante","s_gallina","s_gato",
            "s_rana","s_perro","s_pato","s_oveja","s_leon","s_jirafa",
            "s_raton","s_vaca","s_autobus","s_automovil","s_avion","s_bicicleta","s_camioneta",
            "s_casa","s_celular","s_guitarra","s_motocicleta","s_silla","s_television","s_durazno","s_fresa","s_mango",
            "s_uvas","s_sandia","s_platano","s_coco","s_pera","s_naranja","s_manzana",
            "s_bart","s_batman","s_cerebro","s_chavo","s_goku","s_homero","s_marge",
            "s_patricio","s_pepa","s_phineas","s_quico","s_spiderman","s_thor","s_superman"};

    public static boolean[] estado={false,false,false,false,false,false,
            false,false,false,false,false,false,
            false,false,false,false,false,false,false,
            false,false,false,false,false,false,false,false,
            false,false,false,false,false,false,false,
            false,false,false,false,false,false,false,false,false,false,
            false,false,false,false,false,false};

    public static int cosas_adivinadas=0;
    private int intentos=3;
    private Button aceptar;
    private TextView mensaje_intentos,mensaje_cuenta;
    private EditText usuario_cosa;
    private int numero_generado=0;
    private ImageView miimagen;
    private MediaPlayer reproductor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_categoria);
        aceptar=(Button) findViewById(R.id.btnaceptar);
        mensaje_intentos=(TextView) findViewById(R.id.lblintentos);
        mensaje_cuenta=(TextView) findViewById(R.id.lblcuenta);
        usuario_cosa=(EditText) findViewById(R.id.txtcosa);
        miimagen=(ImageView) findViewById(R.id.imgcosa);
        CargarPreferencias();
        new MiTarea().execute();
        reproductor= MediaPlayer.create(this,R.raw.yansha);
        reproductor.setLooping(true);
        reproductor.start();
        mensaje_intentos.setText("Tiene " + intentos + " intentos");
        aceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String nombre=usuario_cosa.getText().toString().toLowerCase();
                if(nombre.equals(nombre_cosa[numero_generado]))
                {
                    establecer_cosa(numero_generado);
                    estado[numero_generado]=true;
                    cosas_adivinadas++;
                    esperar();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Incorrecto", Toast.LENGTH_SHORT).show();
                    intentos=intentos-1;
                    mensaje_intentos.setText("Tiene " + intentos + " intentos");
                }

                if (intentos==0)
                {
                    removerPreferencias();
                    Intent i = new Intent(Categoria.this,Perder.class);
                    startActivity(i);
                    finish();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        reproductor.start();
    }

    public void esperar()
    {
        new CountDownTimer(5000,1000)
        {

            @Override
            public void onTick(long millisUntilFinished) {
                mensaje_cuenta.setText("Generando en " + (millisUntilFinished/1000));
            }

            @Override
            public void onFinish() {

                if (cosas_adivinadas==nombre_cosa.length)
                {
                    finish();
                }
                else
                {
                    new MiTarea().execute();
                    mensaje_cuenta.setText("");
                    usuario_cosa.setText("");
                }
            }
        }.start();
    }

    public void CargarPreferencias()
    {
        SharedPreferences mispreferencias = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
        intentos=mispreferencias.getInt("intentos",3);
        cosas_adivinadas=mispreferencias.getInt("adivinados",0);
        for (int i=0;i<nombre_cosa.length;i++)
        {
            estado[i]=mispreferencias.getBoolean(nombre_cosa[i],false);
        }
    }

    public void GuardarPreferencias()
    {
        SharedPreferences mispreferencias = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = mispreferencias.edit();
        editor.putInt("intentos",intentos);
        editor.putInt("adivinados",cosas_adivinadas);
        for (int i=0;i<nombre_cosa.length;i++)
        {
            editor.putBoolean(nombre_cosa[i], estado[i]);
        }
        editor.commit();
    }

    private void establecer_cosa(int numero)
    {
        int resId = getResources().getIdentifier(nombre_cosa[numero], "drawable", getPackageName());
        miimagen.setImageResource(resId);
    }

    private void establecer_sombra(int numero)
    {
        int resId = getResources().getIdentifier(sombra_cosa[numero], "drawable", getPackageName());
        miimagen.setImageResource(resId);
    }


    private void removerPreferencias()
    {
        SharedPreferences settings = getSharedPreferences("PreferenciaCosa", Context.MODE_PRIVATE);
        settings.edit().clear().commit();
    }

    @Override
    protected void onStop() {
        if (intentos==0)
        {
            removerPreferencias();
        }
        else
        {
            GuardarPreferencias();
        }
        reproductor.pause();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (reproductor.isPlaying())
        {
            reproductor.stop();
            reproductor.release();
        }
        super.onDestroy();
    }

    private class MiTarea extends AsyncTask<Void, Void, Void> {
        private int valor_generado;
        @Override
        protected Void doInBackground(Void... params) {
            do {
                valor_generado=((int)(Math.random()*nombre_cosa.length));
            }while(estado[valor_generado]);
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            numero_generado = valor_generado;
            establecer_sombra(valor_generado);
            super.onPostExecute(aVoid);
        }
    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create a variable like this:

boolean isTextCorrect = false;

In default, the button is invisible. Implement a listener. If the text in the textfield changed and its correct switch isTextCorrect to true and make the button visible (clickable).

if(isTextCorrect){
    button.setVisible(View.VISIBLE);
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...