随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
平时很少接触到这个控件,它是TextView的子类,但是可以实现选中,像CheckBox一样,个人感觉它就像是TextView和CheckBox的综合,使用非常简单,代码如下:
MainActivity:
package com.home.testcheckedtextview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckedTextView;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener {
private CheckedTextView ctv1;
private CheckedTextView ctv2;
private boolean ctv2Checked = false;
public ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initWidget();
}
private void initWidget() {
ctv1 = (CheckedTextView) findViewById(R.id.main_ctv1);
ctv1.setOnClickListener(this);
ctv1.setChecked(true);// 设置为选中,不设置默认为不选中
ctv2 = (CheckedTextView) findViewById(R.id.main_ctv2);
ctv2.setOnClickListener(this);
// 更改图标
ctv2.setCheckMarkDrawable(android.R.drawable.arrow_down_float);
listView = (ListView) findViewById(R.id.main_lv);
listView.setAdapter(new MyListViewAdapter(this));
}
@Override
public void onClick(View v) {
if (v == ctv1) {
ctv1.toggle();// 反转状态
} else if (v == ctv2) {
ctv2Checked = !ctv2Checked;
changeCtv2State();
}
}
private void changeCtv2State() {
if (ctv2Checked) {
ctv2.setCheckMarkDrawable(android.R.drawable.arrow_up_float);
} else {
ctv2.setCheckMarkDrawable(android.R.drawable.arrow_down_float);
}
}
}
MyListViewAdapter:
package com.home.testcheckedtextview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
public class MyListViewAdapter extends BaseAdapter {
private Context context;
public MyListViewAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 10;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View arg1, ViewGroup viewGroup) {
View view = LayoutInflater.from(context).inflate(R.layout.list_row,
null);
final CheckedTextView ctv = (CheckedTextView) view
.findViewById(R.id.list_row_ctv);
ctv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ctv.toggle();// 反转
}
});
return view;
}
}
布局main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="@+id/main_ctv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_horizontal"
android:text="CheckedTextView1" />
<CheckedTextView
android:id="@+id/main_ctv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_horizontal"
android:text="CheckedTextView2" />
<ListView
android:id="@+id/main_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp" />
</LinearLayout>
ListView每一行布局:list_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="@+id/list_row_ctv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_horizontal"
android:text="CheckedTextView" />
</LinearLayout>