4.6? Checkbox(復(fù)選框)
2.CheckBox (復(fù)選框)
如題,復(fù)選框,即可以同時選中多個選項(xiàng),至于獲得選中的值,同樣有兩種方式:
1.為每個CheckBox添加事件:setOnCheckedChangeListener
2.弄一個按鈕,在點(diǎn)擊后,對每個checkbox進(jìn)行判斷:isChecked();
check_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇水果:"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="香蕉??"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="蘋果??"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/strawberry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="草莓??"
android:textSize="30sp">
</CheckBox>
<Button
android:id="@+id/btnpost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30sp"/>
</LinearLayout>
?
?MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener {
private CheckBox cb1;
private CheckBox cb2;
private CheckBox cb3;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
cb1 = (CheckBox) findViewById(R.id.banana);
cb2 = (CheckBox) findViewById(R.id.apple);
cb3 = (CheckBox) findViewById(R.id.strawberry);
btn_send = (Button) findViewById(R.id.btnpost);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
btn_send.setOnClickListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton,boolean b){
if(compoundButton.isChecked())
Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View view){
String choose = " ";
if(cb1.isChecked())
choose += cb1.getText().toString() + " ";
if (cb2.isChecked())
choose += cb2.getText().toString() + " ";
if(cb3.isChecked())
choose += cb3.getText().toString() + " ";
Toast.makeText(this,choose,Toast.LENGTH_LONG).show();
}
}
進(jìn)行運(yùn)行測試:
選中香蕉??/蘋果??/草莓??:
?提交:
- 自定義點(diǎn)擊效果:
在res下的drawable文件夾下新建checkbox.xml文件。
?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_checked="true"
android:drawable="@mipmap/weixin"/>
<item
android:state_enabled="true"
android:state_checked="false"
android:drawable="@mipmap/wode"/>
</selector>
寫好后,我們有兩種方法設(shè)置,也可以說一種吧!你看看就知道了~
①android:button屬性設(shè)置為上述的selctor
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇水果:"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="香蕉??"
android:button="@drawable/checkbox"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="蘋果??"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/strawberry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="草莓??"
android:textSize="30sp">
</CheckBox>
<Button
android:id="@+id/btnpost2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30sp"
/>
</LinearLayout>
?
②在style中定義一個屬性,然后通過android style屬性設(shè)置,先往style添加下述代碼:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox</item>
</style>
</resources>
?然后布局那里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇水果:"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="香蕉??"
android:button="@drawable/checkbox"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="蘋果??"
android:textSize="30sp"
style="@style/checkbox">
</CheckBox>
<CheckBox
android:id="@+id/strawberry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="草莓??"
android:textSize="30sp">
</CheckBox>
<Button
android:id="@+id/btnpost2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30sp"
/>
</LinearLayout>
?文章來源:http://www.zghlxwxcb.cn/news/detail-469457.html
- 修改文字與選擇框的距離
android:background="@null"
android:paddingLeft="100dp"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇水果:"
android:textSize="30sp"/>
<CheckBox
android:id="@+id/banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="香蕉??"
android:button="@drawable/checkbox"
android:textSize="30sp">
</CheckBox>
<CheckBox
android:id="@+id/apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="蘋果??"
android:textSize="30sp"
style="@style/checkbox">
</CheckBox>
<CheckBox
android:id="@+id/strawberry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="草莓??"
android:textSize="30sp"
android:background="@null"
android:paddingLeft="100dp">
</CheckBox>
<Button
android:id="@+id/btnpost2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="30sp"
/>
</LinearLayout>
?文章來源地址http://www.zghlxwxcb.cn/news/detail-469457.html
到了這里,關(guān)于Android開發(fā)-Android常用組件-Checkbox復(fù)選框的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!