国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android開發(fā)-Android常用組件-Checkbox復(fù)選框

這篇具有很好參考價值的文章主要介紹了Android開發(fā)-Android常用組件-Checkbox復(fù)選框。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

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>

?Android開發(fā)-Android常用組件-Checkbox復(fù)選框

?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)行測試:

選中香蕉??/蘋果??/草莓??:

Android開發(fā)-Android常用組件-Checkbox復(fù)選框Android開發(fā)-Android常用組件-Checkbox復(fù)選框Android開發(fā)-Android常用組件-Checkbox復(fù)選框

?提交:

Android開發(fā)-Android常用組件-Checkbox復(fù)選框

  • 自定義點(diǎn)擊效果:

在res下的drawable文件夾下新建checkbox.xml文件。

Android開發(fā)-Android常用組件-Checkbox復(fù)選框

?Android開發(fā)-Android常用組件-Checkbox復(fù)選框

<?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>

?Android開發(fā)-Android常用組件-Checkbox復(fù)選框Android開發(fā)-Android常用組件-Checkbox復(fù)選框

②在style中定義一個屬性,然后通過android style屬性設(shè)置,先往style添加下述代碼:

?
Android開發(fā)-Android常用組件-Checkbox復(fù)選框
<?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>

?Android開發(fā)-Android常用組件-Checkbox復(fù)選框Android開發(fā)-Android常用組件-Checkbox復(fù)選框

  • 修改文字與選擇框的距離
?
        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>

?Android開發(fā)-Android常用組件-Checkbox復(fù)選框文章來源地址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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包