Android自定義CheckBox含代碼

Android自定義CheckBox

Android提供了自定義視圖元素的界面的功能,而不是默認的界面。愛掏網(wǎng) - it200.com

您可以在Android中創(chuàng)建自定義的CheckBox。愛掏網(wǎng) - it200.com因此,您可以在布局中添加一些不同的復選框圖片。愛掏網(wǎng) - it200.com

自定義CheckBox示例

在此示例中,我們創(chuàng)建了默認的復選框和自定義的復選框。愛掏網(wǎng) - it200.com將以下代碼添加到activity_main.xml文件中。愛掏網(wǎng) - it200.com

activity_main.xml

文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout 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:layout_height="match_parent"  
    tools:context="example.javatpoint.com.customcheckbox.MainActivity">  


    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:gravity="center_horizontal"  
        android:textSize="25dp"  
        android:text="Default Check Box"  
        android:layout_alignParentTop="true"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentStart="true" />  

    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="New CheckBox"  
        android:id="@+id/checkBox"  
        android:layout_below="@+id/textView1"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="46dp" />  

    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="New CheckBox"  
        android:id="@+id/checkBox2"  
        android:layout_below="@+id/checkBox"  
        android:layout_alignLeft="@+id/checkBox"  
        android:layout_alignStart="@+id/checkBox" />  

    <View  
        android:layout_width="fill_parent"  
        android:layout_height="1dp"  
        android:layout_marginTop="200dp"  
        android:background="#B8B894"  
        android:id="@+id/viewStub" />  

    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="CheckBox 1"  
        android:id="@+id/checkBox3"  
        android:button="@drawable/customcheckbox"  
        android:layout_below="@+id/viewStub"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="58dp" />  

    <CheckBox  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="CheckBox 2"  
        android:id="@+id/checkBox4"  
        android:button="@drawable/customcheckbox"  
        android:layout_below="@+id/checkBox3"  
        android:layout_alignLeft="@+id/checkBox3"  
        android:layout_alignStart="@+id/checkBox3" />  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textAppearance="?android:attr/textAppearanceSmall"  
        android:textSize="25dp"  
        android:text="Custom Check Box"  
        android:id="@+id/textView"  
        android:layout_alignTop="@+id/viewStub"  
        android:layout_centerHorizontal="true" />  

    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Show Checked"  
        android:id="@+id/button"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true" />  

</RelativeLayout>  

現(xiàn)在在drawable文件夾下的另一個文件(checkbox.xml)中實現(xiàn)一個選擇器,自定義復選框。愛掏網(wǎng) - it200.com

checkbox.xml

文件:checkbox.xml

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_checked="true" android:drawable="@drawable/checked" />  
    <item android:state_checked="false" android:drawable="@drawable/unchecked"/>  
</selector>  

Activity類

文件:MainActivity.java

package example.javatpoint.com.customcheckbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox cb1,cb2;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cb1=(CheckBox)findViewById(R.id.checkBox3);
        cb2=(CheckBox)findViewById(R.id.checkBox4);
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb=new StringBuilder("");

                if(cb1.isChecked()){
                    String s1=cb1.getText().toString();
                    sb.append(s1);
                }

                if(cb2.isChecked()){
                    String s2=cb2.getText().toString();
                    sb.append("\n"+s2);

                }
                if(sb!=null && !sb.toString().equals("")){
                    Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();

                }
                else{
                    Toast.makeText(getApplicationContext(),"Nothing Selected", Toast.LENGTH_LONG).show();
                }

            }

        });
    }
}

輸出

聲明:所有內容來自互聯(lián)網(wǎng)搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 欧美国产日韩在线| 91香蕉视频直播| 西西人体免费视频| 日韩免费观看一级毛片看看| 国产私拍福利精品视频| 夜夜偷天天爽夜夜爱| 免费能直接在线观看黄的视频 | 欧美jizz18| 国产精品无码久久久久久| 亚洲欧美日韩国产精品久久| 亚洲中文字幕在线观看| 777久久精品一区二区三区无码 | 无套后进式视频在线观看| 国产三级视频在线| 中文字幕欧美激情| 美女扒开胸露出奶乳免费视频| 成年人免费观看视频网站| 又嫩又硬又黄又爽的视频| 一级特黄录像视频免费| 精品一区二区久久久久久久网站 | 日本熟妇色熟妇在线视频播放 | 成人a在线观看| 国产成人精品福利色多多| 亚洲神级电影国语版| 91成年人免费视频| 欧美办公室系列观看丝袜| 国产欧美日韩精品一区二区三区| 乱人伦人妻中文字幕无码| 韩国黄色片在线观看| 欧美大交乱xxxx| 在线一区免费视频播放| 亚洲成av人影片在线观看| 麻豆狠色伊人亚洲综合网站| 日本高清无卡码一区二区久久| 四虎成人精品国产永久免费无码| 一日本道a高清免费播放| 波多野结衣导航| 国产真人无码作爱免费视频| 久久精品99久久香蕉国产| 美国式禁忌在线播放| 在线看亚洲十八禁网站|