Home Menu

Menu



Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android


 
Herramientas
  #1  
Viejo 04/03/17, 04:00:15
Avatar de Elfomask
Elfomask Elfomask no está en línea
Usuario poco activo
Mensajes: 2
 
Fecha de registro: mar 2017
Mensajes: 2
Tu operador: Movistar
Mencionado: 0 comentarios
Tagged: 0 hilos
Como usar el o los botones de mis "handfree" en lugar de un "button"?

Buenos días, buenas tardes/noches.
Disculpen si el tema esta mal posteado o si eh cometido un error.

Soy nuevo con android, en realidad no se programar lo que eh logrado hasta ahora es a base de mirar videos y prueva /error. Pero creo que para esto necesito ayuda de gente versada en esto de programar.

Entrando en tema, mi aplicacion es simple, un cronometro que empiesa a contar y dependiendo de en que número el cronometro se detiene muestra un mensaje acorde con ese número. Actualmente he logrado, y no se como, que el cronometro inicie y se detenga tocando la pantalla, pero resulta que ahora necesito que se pueda iniciar con un boton fisico y en aras de no tener que destrozar mi celular eh llegado a la conclusion que la mejor opcion es usar el boton de mis audifonos ya viejos.

Estoy usando android estudios
MI aplicacion es compatible con o desde android 4.3(realmente no se mucho de esto)
Este es le codigo de mi MainActivity Java
Código:
public class MainActivity extends AppCompatActivity implements OnTouchListener, View.OnClickListener {

    TextView tempo;
    TextView informacion;
    LinearLayout capa1;
    LinearLayout capa2;
    LinearLayout master;
    Button push;
    Chronometer clock;
    public int time = 0;
    public int showtime = 0;
    public int vez = 0;

    String mensaje1;
    String contador = Integer.toString(showtime);


    @override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        informacion = (TextView) findViewById(R.id.informacion);
        tempo = (TextView) findViewById(R.id.tempo);
        push = (Button) findViewById(R.id.start);
        clock = (Chronometer) findViewById(R.id.clock);
        capa1 = (LinearLayout) this.findViewById(R.id.capa1);
        capa2 = (LinearLayout) this.findViewById(R.id.capa2);
        master = (LinearLayout) this.findViewById(R.id.master);

        master.setOnTouchListener(this);

        //reloj
        clock.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @override
            public void onChronometerTick(Chronometer chronometer) {

                time = time + 1;
                if (time == 2) {
                    time = 0;
                    showtime = showtime + 1;
                }
                tempo.setText(contador = Integer.toString(showtime));
            }
        });
        //final de reloj








        //cuando tocas la pantalla
        master.setOnClickListener(new View.OnClickListener() {
            @override
            public void onClick(View v) {
                if (vez == 0) {
                    capa1.setVisibility(View.VISIBLE);
                    capa2.setVisibility(View.GONE);
                    tempo.setText("0");
                    time = 0;
                    showtime = 0;
                    clock.setBase(SystemClock.elapsedRealtime());
                    clock.start();
                    vez = 1;
                } else if (vez == 1) {
                    clock.stop();
                    if (vez == 1 && showtime > 0) {
                        capa1.setVisibility(View.GONE);
                        capa2.setVisibility(View.VISIBLE);


                        if (showtime > 0) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 2) {
                            informacion.setText(getString(R.string.mensaje2));
                        }
                        if (showtime == 3) {
                            informacion.setText(getString(R.string.mensaje3));
                        }
                        if (showtime == 4) {
                            informacion.setText(getString(R.string.mensaje4));
                        }
                        if (showtime == 5) {
                            informacion.setText(getString(R.string.mensaje5));
                        }
                        if (showtime == 6) {
                            informacion.setText(getString(R.string.mensaje6));
                        }
                        if (showtime == 7) {
                            informacion.setText(getString(R.string.mensaje7));
                        }
                        if (showtime == 8) {
                            informacion.setText(getString(R.string.mensaje8));
                        }
                        if (showtime == 9) {
                            informacion.setText(getString(R.string.mensaje9));
                        }
                        if (showtime == 10) {
                            informacion.setText(getString(R.string.mensaje10));
                        }
                        if (showtime == 11) {
                            informacion.setText(getString(R.string.mensaje11));
                        }
                        if (showtime == 12) {
                            informacion.setText(getString(R.string.mensaje12));
                        }
                        if (showtime == 13) {
                            informacion.setText(getString(R.string.mensaje13));
                        }
                        if (showtime == 14) {
                            informacion.setText(getString(R.string.mensaje14));
                        }
                        if (showtime == 15) {
                            informacion.setText(getString(R.string.mensaje15));
                        }
                        if (showtime == 16) {
                            informacion.setText(getString(R.string.mensaje16));
                        }
                        if (showtime == 17) {
                            informacion.setText(getString(R.string.mensaje17));
                        }
                        if (showtime == 18) {
                            informacion.setText(getString(R.string.mensaje18));
                        }
                        if (showtime == 19) {
                            informacion.setText(getString(R.string.mensaje19));
                        }
                        if (showtime == 20) {
                            informacion.setText(getString(R.string.mensaje20));
                        }
                        if (showtime == 21) {
                            informacion.setText(getString(R.string.mensaje21));
                        }
                        if (showtime == 22) {
                            informacion.setText(getString(R.string.mensaje22));
                        }
                        if (showtime == 23) {
                            informacion.setText(getString(R.string.mensaje23));
                        }
                        if (showtime == 24) {
                            informacion.setText(getString(R.string.mensaje24));
                        }
                        if (showtime == 25) {
                            informacion.setText(getString(R.string.mensaje25));
                        }
                        if (showtime == 26) {
                            informacion.setText(getString(R.string.mensaje26));
                        }
                        if (showtime == 27) {
                            informacion.setText(getString(R.string.mensaje27));
                        }
                        if (showtime == 28) {
                            informacion.setText(getString(R.string.mensaje28));
                        }
                        if (showtime == 29) {
                            informacion.setText(getString(R.string.mensaje29));
                        }
                        if (showtime == 30) {
                            informacion.setText(getString(R.string.mensaje30));
                        }
                        if (showtime == 31) {
                            informacion.setText(getString(R.string.mensaje31));
                        }
                        if (showtime == 32) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 33) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 34) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 35) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 36) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 37) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 38) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 39) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 40) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 41) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 42) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 43) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 44) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 45) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 46) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 47) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 48) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 49) {
                            informacion.setText(getString(R.string.mensaje1));
                        }
                        if (showtime == 50) {
                            informacion.setText(getString(R.string.mensaje1));
                        }


                    }
                    vez = 0;
                }

            }
        });





    }

//aqui termina plublic void


            @override
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction()==MotionEvent.ACTION_UP){
                    if (vez == 0) {
                        capa1.setVisibility(View.VISIBLE);
                        capa2.setVisibility(View.GONE);
                        tempo.setText("0");
                        time = 0;
                        showtime = 0;
                        clock.setBase(SystemClock.elapsedRealtime());
                        clock.start();
                        vez = 1;
                    } else if (vez == 1) {
                        clock.stop();
                        if (vez == 1 && showtime > 0) {
                            capa1.setVisibility(View.GONE);
                            capa2.setVisibility(View.VISIBLE);

                            if (showtime > 0) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 2) {
                                informacion.setText(getString(R.string.mensaje2));
                            }
                            if (showtime == 3) {
                                informacion.setText(getString(R.string.mensaje3));
                            }
                            if (showtime == 4) {
                                informacion.setText(getString(R.string.mensaje4));
                            }
                            if (showtime == 5) {
                                informacion.setText(getString(R.string.mensaje5));
                            }
                            if (showtime == 6) {
                                informacion.setText(getString(R.string.mensaje6));
                            }
                            if (showtime == 7) {
                                informacion.setText(getString(R.string.mensaje7));
                            }
                            if (showtime == 8) {
                                informacion.setText(getString(R.string.mensaje8));
                            }
                            if (showtime == 9) {
                                informacion.setText(getString(R.string.mensaje9));
                            }
                            if (showtime == 10) {
                                informacion.setText(getString(R.string.mensaje10));
                            }
                            if (showtime == 11) {
                                informacion.setText(getString(R.string.mensaje11));
                            }
                            if (showtime == 12) {
                                informacion.setText(getString(R.string.mensaje12));
                            }
                            if (showtime == 13) {
                                informacion.setText(getString(R.string.mensaje13));
                            }
                            if (showtime == 14) {
                                informacion.setText(getString(R.string.mensaje14));
                            }
                            if (showtime == 15) {
                                informacion.setText(getString(R.string.mensaje15));
                            }
                            if (showtime == 16) {
                                informacion.setText(getString(R.string.mensaje16));
                            }
                            if (showtime == 17) {
                                informacion.setText(getString(R.string.mensaje17));
                            }
                            if (showtime == 18) {
                                informacion.setText(getString(R.string.mensaje18));
                            }
                            if (showtime == 19) {
                                informacion.setText(getString(R.string.mensaje19));
                            }
                            if (showtime == 20) {
                                informacion.setText(getString(R.string.mensaje20));
                            }
                            if (showtime == 21) {
                                informacion.setText(getString(R.string.mensaje21));
                            }
                            if (showtime == 22) {
                                informacion.setText(getString(R.string.mensaje22));
                            }
                            if (showtime == 23) {
                                informacion.setText(getString(R.string.mensaje23));
                            }
                            if (showtime == 24) {
                                informacion.setText(getString(R.string.mensaje24));
                            }
                            if (showtime == 25) {
                                informacion.setText(getString(R.string.mensaje25));
                            }
                            if (showtime == 26) {
                                informacion.setText(getString(R.string.mensaje26));
                            }
                            if (showtime == 27) {
                                informacion.setText(getString(R.string.mensaje27));
                            }
                            if (showtime == 28) {
                                informacion.setText(getString(R.string.mensaje28));
                            }
                            if (showtime == 29) {
                                informacion.setText(getString(R.string.mensaje29));
                            }
                            if (showtime == 30) {
                                informacion.setText(getString(R.string.mensaje30));
                            }
                            if (showtime == 31) {
                                informacion.setText(getString(R.string.mensaje31));
                            }
                            if (showtime == 32) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 33) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 34) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 35) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 36) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 37) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 38) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 39) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 40) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 41) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 42) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 43) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 44) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 45) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 46) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 47) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 48) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 49) {
                                informacion.setText(getString(R.string.mensaje1));
                            }
                            if (showtime == 50) {
                                informacion.setText(getString(R.string.mensaje1));
                            }


                        }
                        vez = 0;
                    }
                    return true;
                }
                return false;
            }


    @override
    public void onClick(View v) {
        if (vez == 0) {
            capa1.setVisibility(View.VISIBLE);
            capa2.setVisibility(View.GONE);
            tempo.setText("0");
            time = 0;
            showtime = 0;
            clock.setBase(SystemClock.elapsedRealtime());
            clock.start();
            vez = 1;
        } else if (vez == 1) {
            clock.stop();
            if (vez == 1 && showtime > 0) {
                capa1.setVisibility(View.GONE);
                capa2.setVisibility(View.VISIBLE);


                if (showtime > 0) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 2) {
                    informacion.setText(getString(R.string.mensaje2));
                }
                if (showtime == 3) {
                    informacion.setText(getString(R.string.mensaje3));
                }
                if (showtime == 4) {
                    informacion.setText(getString(R.string.mensaje4));
                }
                if (showtime == 5) {
                    informacion.setText(getString(R.string.mensaje5));
                }
                if (showtime == 6) {
                    informacion.setText(getString(R.string.mensaje6));
                }
                if (showtime == 7) {
                    informacion.setText(getString(R.string.mensaje7));
                }
                if (showtime == 8) {
                    informacion.setText(getString(R.string.mensaje8));
                }
                if (showtime == 9) {
                    informacion.setText(getString(R.string.mensaje9));
                }
                if (showtime == 10) {
                    informacion.setText(getString(R.string.mensaje10));
                }
                if (showtime == 11) {
                    informacion.setText(getString(R.string.mensaje11));
                }
                if (showtime == 12) {
                    informacion.setText(getString(R.string.mensaje12));
                }
                if (showtime == 13) {
                    informacion.setText(getString(R.string.mensaje13));
                }
                if (showtime == 14) {
                    informacion.setText(getString(R.string.mensaje14));
                }
                if (showtime == 15) {
                    informacion.setText(getString(R.string.mensaje15));
                }
                if (showtime == 16) {
                    informacion.setText(getString(R.string.mensaje16));
                }
                if (showtime == 17) {
                    informacion.setText(getString(R.string.mensaje17));
                }
                if (showtime == 18) {
                    informacion.setText(getString(R.string.mensaje18));
                }
                if (showtime == 19) {
                    informacion.setText(getString(R.string.mensaje19));
                }
                if (showtime == 20) {
                    informacion.setText(getString(R.string.mensaje20));
                }
                if (showtime == 21) {
                    informacion.setText(getString(R.string.mensaje21));
                }
                if (showtime == 22) {
                    informacion.setText(getString(R.string.mensaje22));
                }
                if (showtime == 23) {
                    informacion.setText(getString(R.string.mensaje23));
                }
                if (showtime == 24) {
                    informacion.setText(getString(R.string.mensaje24));
                }
                if (showtime == 25) {
                    informacion.setText(getString(R.string.mensaje25));
                }
                if (showtime == 26) {
                    informacion.setText(getString(R.string.mensaje26));
                }
                if (showtime == 27) {
                    informacion.setText(getString(R.string.mensaje27));
                }
                if (showtime == 28) {
                    informacion.setText(getString(R.string.mensaje28));
                }
                if (showtime == 29) {
                    informacion.setText(getString(R.string.mensaje29));
                }
                if (showtime == 30) {
                    informacion.setText(getString(R.string.mensaje30));
                }
                if (showtime == 31) {
                    informacion.setText(getString(R.string.mensaje31));
                }
                if (showtime == 32) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 33) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 34) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 35) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 36) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 37) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 38) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 39) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 40) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 41) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 42) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 43) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 44) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 45) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 46) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 47) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 48) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 49) {
                    informacion.setText(getString(R.string.mensaje1));
                }
                if (showtime == 50) {
                    informacion.setText(getString(R.string.mensaje1));
                }


            }
            vez = 0;
        }
    }
}
mi activity_main.xml

Código:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="llpinc.datotiempo.MainActivity"
    android:id="@+id/master"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/start"
        android:text="@string/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>


    <LinearLayout
        android:id="@+id/capa1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="visible"
        android:layout_marginTop="30sp">
    <TextView

        android:id="@+id/tempo"
        android:text="@string/tempo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="160sp"
        android:textStyle="normal|bold"
        android:textAlignment="center" />
    </LinearLayout>




    <LinearLayout
        android:id="@+id/capa2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="invisible"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="30sp"
        >

        <TextView
            android:id="@+id/informacion"
            android:text="@string/informacion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="40sp"
            android:textStyle="normal|italic"
            android:fontFamily="sans-serif"
            android:textAlignment="center" />
    </LinearLayout>

    <Chronometer
        android:id="@+id/clock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:visibility="invisible"/>



</LinearLayout>
Como veran un desastre y mucho codigo repetido pero como ya dije apenas empesé hace 2 dias y estoy algo... totalmente perdido. Agradeseria cualquier pista o ayuda que me pueda dar por donde empezar o terminar.

Gracias desde ya y nuevamente disculpen por cualquier error.
Responder Con Cita


  #2  
Viejo 04/03/17, 09:50:32
Avatar de mocelet
mocelet mocelet no está en línea
Desarrollador
Mensajes: 2,203
 
Fecha de registro: may 2011
Localización: Madrid
Mensajes: 2,203
Tu operador: -
Mencionado: 17 comentarios
Tagged: 2 hilos
SO al rescate, con el onKeyDown y el evento de tecla KeyEvent.KEYCODE_HEADSETHOOK: http://stackoverflow.com/a/41790723/1407634

De todas formas como bien dices hay mucho código parecido e incluso duplicado, y mientras más código más fácil cometer errores o que se te escape un número por ahí. Tan importante es que en pantalla vayan saliendo cosas como ir construyendo una buena base para ir adquiriendo buenas prácticas de programación poco a poco. Ánimo
Responder Con Cita
Gracias de parte de:
  #3  
Viejo 04/03/17, 16:58:32
Avatar de Elfomask
Elfomask Elfomask no está en línea
Usuario poco activo
Mensajes: 2
 
Fecha de registro: mar 2017
Mensajes: 2
Tu operador: Movistar
Mencionado: 0 comentarios
Tagged: 0 hilos
¡Muchas gracias!
Sabia que necesitaba ayuda. Intente de todo hasta ahora, pero al saber que ese era el codigo correcto reduje los intentos a solo ese extracto de codigo y aunque no sabía en que parte insertarlo nuevamente por ensallo y error llegue a mi respuesta.

Una vez mas, muchas gracias mocelet, me has dado la vida.
Y por sobretodo eh aprendido algo más, gracias.

Última edición por Elfomask Día 04/03/17 a las 17:01:53
Responder Con Cita
Respuesta

Estás aquí
Regresar   HTCMania > Todo sobre Android > Programación y Desarrollo para Android


Reglas de Mensajes
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Las caritas están On
Código [IMG] está On
Código HTML está Off

Saltar a Foro



Hora actual: 15:14:43 (GMT +1)

Cookies
Powered by vBulletin™
Copyright © vBulletin Solutions, Inc. All rights reserved.
 
HTCMania: líderes desde el 2007