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

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


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


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

Android开发-CheckedTextView复选框自定义样式-AndroidStudio_第1张图片

res\layout\my_select_dialog_multichoice.xml:



















res\drawable\checkbox_style.xml:



    
    
    
    
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 adapter;
    //private ArrayAdapter adapter;

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

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

        //adapter = new ArrayAdapter(this,R.layout.text_item);
        //adapter = new ArrayAdapter(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:




    

    




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



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

微信:iwanghang
QQ:413711276
邮箱:[email protected]



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

你可能感兴趣的:(Android,Android基础控件,Android基础控件)