Android – Kontrollkästchen der Grundsteuerung: CheckBox (8)

1.Wissenspunkte

(1) Beherrschen Sie die Verwendung von CheckBox-Komponenten.

2. Spezifischer Inhalt

 

 Beispiel: Lassen Sie Benutzer Interessen und Hobbys auswählen

<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">
    <TextView
        android:id="@+id/myinit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择兴趣爱好" />
	<CheckBox 
	    android:id="@+id/inti1"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打篮球"
	    />
	<CheckBox 
	    android:id="@+id/inti2"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="唱歌"
	    />
	<CheckBox 
	    android:id="@+id/inti3"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳舞"
	    />
	<CheckBox 
	    android:id="@+id/inti4"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="散打"
	    />
</LinearLayout>

Jetzt ist der angezeigte Effekt vorhanden, aber es gibt keine Standardauswahl. Jetzt können wir ihn über die Layoutdatei oder das Aktivitätsprogramm festlegen.

<CheckBox 
	    android:id="@+id/inti2"
	    android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="唱歌"
        android:checked="true"—设置默认选中
	    />

Jetzt richten wir es über das Programm ein.

public class CheckBoxActivity extends Activity {
	private CheckBox init3 = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox_check_box);
        this.init3 = (CheckBox) super.findViewById(R.id.inti3);
        this.init3.setChecked(true);//默认选中
    }
}

3. Zusammenfassung

Die CheckBox-Komponente wird verwendet, um die Funktion eines Kontrollkästchens zu implementieren.

Supongo que te gusta

Origin blog.csdn.net/weixin_41830242/article/details/131205259
Recomendado
Clasificación