Android API之CheckedTextView代码演示

<p>类CheckedTextView继承超类TextView并实现Checkable接口。当ListView的setChoiceMode方法并设定为CHOICE_MODE_SINGLE或者CHOICE_MODE_MULTIPLE,而非CHOICE_MODE_NONE时,使用此类是很有用的。</p><p>使用范例:</p><p>(1)</p><p>/res/layout/main.xml 中添加相应资源ID</p><pre class="brush:xml;toolbar:false">&lt;ListView android:id=&quot;@+id/listView&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; /&gt; &lt;CheckedTextView android:id=&quot;@+id/checkedTextView1&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:checkMark=&quot;?android:attr/listChoiceIndicatorMultiple&quot; android:text=&quot;@string/checkedTextView1&quot; /&gt; &lt;CheckedTextView android:id=&quot;@+id/checkedTextView2&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:checkMark=&quot;?android:attr/listChoiceIndicatorMultiple&quot; android:text=&quot;@string/checkedTextView2&quot; /&gt; &lt;CheckedTextView android:id=&quot;@+id/checkedTextView3&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:checkMark=&quot;?android:attr/listChoiceIndicatorMultiple&quot; android:text=&quot;@string/checkedTextView3&quot; /&gt; &lt;CheckedTextView android:id=&quot;@+id/checkedTextView4&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:checkMark=&quot;?android:attr/listChoiceIndicatorMultiple&quot; android:text=&quot;@string/checkedTextView4&quot; /&gt;</pre><p>(2)/res/values/strings.xml 资源</p><pre class="brush:xml;toolbar:false">&lt;stringname=&quot;hello&quot;&gt;TextView多选框&lt;/string&gt; &lt;stringname=&quot;app_name&quot;&gt;CheckedTextView&lt;/string&gt; &lt;stringname=&quot;checkedTextView1&quot;&gt;TextView多选框1&lt;/string&gt; &lt;stringname=&quot;checkedTextView2&quot;&gt;TextView多选框2&lt;/string&gt; &lt;stringname=&quot;checkedTextView3&quot;&gt;TextView多选框3&lt;/string&gt; &lt;stringname=&quot;checkedTextView4&quot;&gt;TextView多选框4&lt;/string&gt;</pre><p></p><p>(3)源代码中使用</p><pre class="brush:java;toolbar:false">listView=(ListView)findViewById(R.id.listView); checkedTextView1=(CheckedTextView)findViewById(R.id.checkedTextView1); checkedTextView2=(CheckedTextView)findViewById(R.id.checkedTextView2); checkedTextView3=(CheckedTextView)findViewById(R.id.checkedTextView3); checkedTextView4=(CheckedTextView)findViewById(R.id.checkedTextView4); //设置checkedTextView1为选中状态 checkedTextView1.setChecked(true); //设置checkedTextView2的页边距,即距上/下/左/右各20像素,默认为未选中状态 checkedTextView2.setPadding(20,20,20,20); //设置checkedTextView3为选中状态,并更改其显示图标,使用android系统资源arrow_down_float checkedTextView3.setChecked(true); checkedTextView3.setCheckMarkDrawable(android.R.drawable.arrow_down_float); //设置checkedTextView4反转状态,由默认的未选中反转为选中状态 checkedTextView4.toggle(); //点击状态后变更相反,如选中变为未选中,未选中的变为选中 checkedTextView1.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub checkedTextView1.toggle(); } }); //点击状态后变更相反,如选中变为未选中,未选中的变为选中 checkedTextView2.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub checkedTextView2.toggle(); } }); //点击状态后变更相反,即下三角转化为上三角符号 checkedTextView3.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub checkedTextView3.setCheckMarkDrawable(android.R.drawable.arrow_up_float); } }); //点击状态后变更相反,如选中变为未选中,未选中的变为选中 checkedTextView4.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub checkedTextView4.toggle(); } }); //设置listView的模式为CHOICE_MODE_SINGLE listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);</pre><p>效果截图:Android &lt;wbr&gt;API之CheckedTextView代码演示</p>
RangeTime:0.006765s
RangeMem:211.55 KB
返回顶部 留言