Android CheckedTextView


public class
CheckedTextView

extends  TextView 
implements  Checkable
java.lang.Object
   ↳	android.view.View
 	   ↳	android.widget.TextView
 	 	   ↳	android.widget.CheckedTextView

CheckedTextView继承TextView并实现Checkable接口。当ListViewsetChoiceMode方法并设定为CHOICE_MODE_SINGLE或者 CHOICE_MODE_MULTIPLE,而非CHOICE_MODE_NONE时,使用此类是很有用的。 类对应方法:

公共方法:

public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)

在子视图的构建时分派一个辅助事件。(译者注:通过源码可以看出,视图构建时设置其选中状态。)

参数    event 事件

返回值    如果事件处理完成,则返回true

booleanisChecked();判断是否被选中

  

voidsetPadding(int left, int top, int right, int bottom)
设置边距.

voidsetChecked(boolean checked)

Changes the checked state of this text view. 

设置文本的是否选中状态

voidsetCheckMarkDrawable(int resid)
Set the checkmark to a given Drawable, identified by its resourece id. 
 通过id给Drawable设定一个检查标签

voidsetCheckMarkDrawable(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.执行后的效果:




转载于:https://my.oschina.net/amigos/blog/59418

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值