- TextView: Per mostra textos a l'usuari
Per definir el textView en un
Layout
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nom"
/>
Per accedir des del codi java
TextView tx = (TextView)findViewById(R.id.textView1);
- EditText: per introduir i editar text per part de l'usuari
Per definir-lo en un Layout
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10">
</EditText>
Per accedir des del codi java
EditText edtx = (EditText)findViewById(R.id.editText1);
- Button
Per definir-lo en un layout
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/acceptar"
/>
Per accedir des del codi java
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO
Auto-generated method stub
}
});
- CheckBox : S'usa per marcar o desmarcar una o més opcions
Per definir-lo en un layout
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cine" />
Per accedir des de java, per saber
si està o no marcat
CheckBox checkBox = (CheckBox)
findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
// El checkbox esta marcado
}
else {
// El checkbox esta desmarcado
}
}
});
- RadioButton: serveix per marcar una opció dins d'un grup. Cal incloure dins d'un RadioGroup
Per definir-lo en un layout
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/smx" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/asix" />
</RadioGroup>
Per accedir des de java, per saber
si està o no marcat
RadioGroup
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Hacer
algo con la opción marcada
// representada por el id
checkedId
RadioButton
rb = (RadioButton) findViewById(checkedId);
}
});
Amb el mètode getCheckedRadioButtonId() podem saber quina de les opcions el
radioButton han estat clicades.
Cap comentari:
Publica un comentari a l'entrada