Android开发-CheckedTextView复选框自定义样式-AndroidStudio

转载请注明出处: http://blog.csdn.net/iwanghang/article/details/70053816
觉得博文有用,请点赞,请评论,请关注,谢谢!~


百度了一大圈,其实也没有好的方法,就是selector的练习~~


老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:


res\layout\my_select_dialog_multichoice.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:drawableLeft="@drawable/checkbox_style"

    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="?android:attr/textColorAlertDialogListItem"
    android:gravity="center_vertical"
    android:ellipsize="marquee">

</CheckedTextView>

<!--文字居中-->
<!--android:textAlignment="center"-->

<!--复选框 左显示-->
<!--android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"-->

<!--复选框右显示(默认位置)-->
<!--android:checkMark="?android:attr/listChoiceIndicatorMultiple"-->

<!--自定义的复选框样式 左显示-->
<!--android:drawableLeft="@drawable/checkbox_style"-->

<!--自定义的复选框样式 (默认位置)-->
<!--android:checkMark="@drawable/checkbox_style"-->
res\drawable\checkbox_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/check_nnn" android:state_checked="false"></item>
    <item android:drawable="@drawable/check_nnn" android:state_selected="true"></item>
    <item android:drawable="@drawable/check_nnn" android:state_pressed="true"></item>
    <item android:drawable="@drawable/check_yyy"></item>
</selector>
MainActivity.java:

package com.iwanghang.listviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private ArrayAdapter<String> adapter;
    //private ArrayAdapter<ListData> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 经典的adndroid.R的item
        //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        // 多选item
        //adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice);
        // 自己写的item 使用CheckedTextView
        adapter = new ArrayAdapter<String>(this,R.layout.my_select_dialog_multichoice);
        // 单选item (也可以当多选用)
        //adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice);
        // 勾选item
        //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked);
        // 小字体item
        //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item);

        //adapter = new ArrayAdapter<String>(this,R.layout.text_item);
        //adapter = new ArrayAdapter<ListData>(this,R.layout.text_item);
        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(adapter);
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");
        adapter.add("hello");

        //adapter.add(new ListData("android","man",404));
        //adapter.add(new ListData("android","woman",388));
        //adapter.add(new ListData("android","null",888));

        // 其中 多选、单选、勾选 可以用下面2个函数还设置 多选/单选 属性
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // 多选
        //lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 单选


//        checkedTextView = (CheckedTextView) findViewById(R.layout.my_select_dialog_multichoice);
//        //checkedTextView.setCheckMarkDrawable(android.R.drawable.arrow_down_float);
//
//        //根据数组id得到数组类型
//        TypedArray ta = getBaseContext().getTheme().obtainStyledAttributes(new int[]{R.drawable.icon_box_empty});
//        //初始化绘制目标
//        Drawable indicator = ta.getDrawable(0);
//        checkedTextView = new CheckedTextView(this);
//        checkedTextView.setText("test1");
//        //得到绘制目标后放入选择框中
//        checkedTextView.setCheckMarkDrawable(indicator);




        // item点击监听
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // 写法1:
                //String name = adapter.getItem(position).getUserName();
                //String sex = adapter.getItem(position).getSex();
                //int age = adapter.getItem(position).getAge();
                //Toast.makeText(MainActivity.this, "你点击了:"+name+"  "+sex+"  "+age, Toast.LENGTH_SHORT).show();

                // 写法2:
                //ListData ld = adapter.getItem(position);
                //Toast.makeText(MainActivity.this, String.format("%s  %s  %s",ld.getUserName(),ld.getSex(),ld.getAge()), Toast.LENGTH_SHORT).show();

            }
        });
    }


}
res\layout\activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.iwanghang.listviewdemo.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:textColor="#000000"
        android:text="CheckedTextViewDemo" />

    <ListView
        android:layout_below="@id/tv"
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</RelativeLayout>



转载请注明出处: http://blog.csdn.net/iwanghang/article/details/70053816



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值