public class
CheckedTextView
extends TextView
implements Checkable
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.CheckedTextView
类CheckedTextView继承TextView并实现Checkable接口。当ListView的setChoiceMode方法并设定为CHOICE_MODE_SINGLE或者 CHOICE_MODE_MULTIPLE,而非CHOICE_MODE_NONE时,使用此类是很有用的。 类对应方法:
公共方法:
public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)
在子视图的构建时分派一个辅助事件。(译者注:通过源码可以看出,视图构建时设置其选中状态。)
参数 event 事件
返回值 如果事件处理完成,则返回true
boolean | isChecked();判断是否被选中 |
void | setPadding(int left, int top, int right, int bottom)
设置边距.
|
void | setChecked(boolean checked)
Changes the checked state of this text view. 设置文本的是否选中状态 |
void | setCheckMarkDrawable(int resid)
Set the checkmark to a given Drawable, identified by its resourece id.
通过id给Drawable设定一个检查标签
|
void | setCheckMarkDrawable(Drawable d)
Set the checkmark to a given Drawable.
给Drawable设定一个检查标签
|
public void toggle ()
反转当前视图的选中状态案例:
1定义布局文件:checkedtextview.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckedTextView
android:id="@+id/checkedTextView_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="Anroid"
android:textColor="#fc0000"
/>
<CheckedTextView
android:id="@+id/checkedTextView_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="IOS"
android:textColor="#fc0000"
/>
<CheckedTextView
android:id="@+id/checkedTextView_three"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="Windows Phone"
android:textColor="#fc0000"
/>
<CheckedTextView
android:id="@+id/checkedTextView_four"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:text="BlackBerry"
android:textColor="#fc0000"
/>
</LinearLayout>
2.java文件:CheckedTextViewDemo.java
package com.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class CheckedTextViewDemo extends Activity {
private CheckedTextView checkedTextView_one,checkedTextView_two,checkedTextView_three,checkedTextView_four,flag;
String string;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.checkedtextview);
checkedTextView_one =(CheckedTextView)findViewById(R.id.checkedTextView_one);
checkedTextView_two =(CheckedTextView)findViewById(R.id.checkedTextView_two);
checkedTextView_three =(CheckedTextView)findViewById(R.id.checkedTextView_three);
checkedTextView_four =(CheckedTextView)findViewById(R.id.checkedTextView_four);
//设置checkedTextView_one为选中状态
checkedTextView_one.setChecked(true);
// 设置边距
checkedTextView_four.setPadding(20, 20, 20, 20);
checkedTextView_four.setBackgroundResource(R.drawable.webos);
checkedTextView_two.setCheckMarkDrawable(android.R.drawable.ic_input_add);
checkedTextView_one.setOnClickListener(clickListener);
checkedTextView_two.setOnClickListener(clickListener);
checkedTextView_three.setOnClickListener(clickListener);
checkedTextView_four.setOnClickListener(clickListener);
}
//状态切换
private OnClickListener clickListener =new OnClickListener() {
@Override
public void onClick(View v) {
flag=((CheckedTextView)v);
flag.toggle();
if(flag.isChecked())
{
if (flag.getId()==R.id.checkedTextView_two)
{
checkedTextView_two.setCheckMarkDrawable(android.R.drawable.ic_input_add);
}
string=flag.getText().toString()+"被选中了!";
}else {
if (flag.getId()==R.id.checkedTextView_two)
{
checkedTextView_two.setCheckMarkDrawable(android.R.drawable.ic_input_delete);
}
string=flag.getText().toString()+"被取消了!";
}
toast(string.toString());
}
};
private void toast(String str) {
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
3.执行后的效果: