if.. else try..catch







MyActivity.java
public class MyActivity extends ActionBarActivity {

    EditText et;
    Button btnAccion;
    TextView tv;

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

        et = (EditText) findViewById(R.id.etEdad);
        btnAccion = (Button) findViewById(R.id.btnAccion);
        tv = (TextView) findViewById(R.id.tvEdad);

        btnAccion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(getApplicationContext(), "accion", Toast.LENGTH_SHORT).show();
                int auxEntero = 0;
                String aux = et.getText().toString();

                //if(!aux.matches("")){  //aux != null
                try {
                    auxEntero = Integer.parseInt(aux);
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(), "pone un número!!", Toast.LENGTH_SHORT).show();
                };

                if(auxEntero >= 18)
                    tv.setText("es mayor de edad");
                    else
                        tv.setText("es menor de edad");
            }
        });
    }
}

activity_my.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Coloque edad"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etEdad" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accion"
        android:id="@+id/btnAccion" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/tvEdad" />

</LinearLayout>