Vamos a empezar con algo sencillo con lo que practicar y algunos tips generales sobre android a tener en cuenta en el desarrollo de una app.
Aquí tenéis el vídeo completo donde lo explicamos paso a paso y justo debajo os dejo el código fuente de la app para que podáis comprobar en casa si os funciona:
Dependencias para añadir a Gradle:
compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
MainActivity.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @BindView(R.id.txt_texto) TextView txt_texto; @BindView(R.id.btn_click) Button btn_click; @BindView(R.id.btn_click_interface) Button btn_click_interface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); btn_click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { txt_texto.setText(R.string.onclick1); } }); btn_click_interface.setOnClickListener(MainActivity.this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_click_interface: txt_texto.setText(R.string.onclick2); break; } } public void onClickXML(View view){ txt_texto.setText(R.string.onclick3); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:id="@+id/txt_texto" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog" android:id="@+id/btn_click" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clickeame" /> <Button style="@style/Widget.AppCompat.Button.Borderless" android:id="@+id/btn_click_interface" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clickeame Interface" /> <Button android:onClick="onClickXML" style="@style/Widget.AppCompat.Button.Colored" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Clickeame XML"/> </LinearLayout> |