一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Android自定義CheckBox含代碼

Android自定義CheckBox

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

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

自定義CheckBox示例

在此示例中,我們創(chuàng)建了默認(rèn)的復(fù)選框和自定義的復(fù)選框。愛掏網(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)一個選擇器,自定義復(fù)選框。愛掏網(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();
                }

            }

        });
    }
}

輸出

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

返回頂部

主站蜘蛛池模板: 欧美影院 | 成人黄页在线观看 | 天天操操操操操 | 无吗视频| 在线小视频 | 一级毛片免费完整视频 | 欧美综合在线观看 | 成人欧美一区二区三区色青冈 | 国产一区二区不卡 | 蜜桃精品视频在线 | 国产免费a视频 | 国产日韩欧美 | 午夜一级做a爰片久久毛片 精品综合 | 亚洲一区中文字幕在线观看 | 刘亦菲国产毛片bd | 精品久久久久久亚洲综合网 | 国产精品午夜电影 | 欧美国产一区二区 | 亚洲精选一区 | 91免费电影 | 欧美一区二区在线播放 | 欧美精品久久久 | 成人三级av | 在线免费观看成年人视频 | 精品国产91 | 久久久久久国产 | 欧美一区二区另类 | 久久精品一级 | 国产精品一区二区久久 | 色免费看| 日韩一区二区三区在线视频 | 亚洲欧美日韩精品久久亚洲区 | 国产一区91精品张津瑜 | 欧美黄色绿像 | 日韩一级| 97福利在线 | 精品久久久久久亚洲精品 | 人人干人人玩 | 欧美精品一区二区三区在线播放 | 欧美日韩精品一区二区三区视频 | 精品国产一区二区久久 |