最初我想要一个复选标记,其中文本位于复选标记的左侧.在这个网站上搜索后,我发现最好的解决方法是android:CheckedTextView?但是,我发现用户无法手动更改复选标记.它是按设计的吗?
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/autoupdatecheckboxview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:text="Pop up a message when new data available"
android:typeface="sans" android:textSize="16dip"/>
Run Code Online (Sandbox Code Playgroud) 我有一个ListView,显示2种项目.其中一种包含CheckedTextView.作为一个适配器,我正在使用自定义适配器扩展ArrayAdapter,其数据结构包含有关内部已检查/未检查状态的信息.
有些项目被标记为已选中(在我的数据结构中),所以我当然希望在创建ListView时标记复选框.我尝试使用CheckedTextView的setChecked()方法在适配器中的getView()方法上执行此操作,但它不起作用.我发现它应该在ListView级别上使用setItemChecked()来完成它并且它确实有效,但它对我来说没有意义,因为为了使它工作,我必须循环活动的onCreate()中的所有项目调用setItemChecked()对于那些被选中 项目列表可能很长,只选择了少数项目,这是浪费.
为什么在getView()中调用setChecked()不起作用?有没有更好的方法(我是一个Android新手).
下面是我检查的项目布局和适配器.
布局:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textAppearance="@style/listViewItemText"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dp"
android:paddingRight="6dp"
/>
Run Code Online (Sandbox Code Playgroud)
适配器:
package com.melog.re.droid.search;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import com.melog.re.droid.core.R;
import com.melog.re.droid.search.MultiCriterionValue.MultiCriterionSingleValue;
import com.melog.re.droid.search.MultiListAdapter.MultiChoiceItem;
public class MultiListAdapter extends ArrayAdapter<MultiChoiceItem> {
private static final int VIEW_TYPES_COUNT = 2;
public static final int VIEW_TYPE_GROUP = 0;
public static final int VIEW_TYPE_ITEM = 1;
private LayoutInflater …Run Code Online (Sandbox Code Playgroud) 标题不言而喻.我有几篇文章,主题,仍然没有弄清楚如何使用checkedtextview.我想要一个带有可检查项目的列表视图.在下面的代码中,我使用listview并用字符串数组填充它.但是如何将其更改为checkedtextview?
delete.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/linlay0"
android:background="@color/list_bg">
<TextView
android:id="@+id/TextView00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#D8D8D8"
android:textColor="#424242"
android:gravity="center_horizontal|center_vertical"
android:textSize="20px"
android:height="40px"
android:textStyle="bold"
/>
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
delete_lv.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/list_content"
android:textColor="#222222"
android:gravity="center"
android:text="sample"
android:layout_margin="4dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Delete.java:
public class Delete extends Activity {
ListView lv1;
ArrayAdapter<String> adapter1;
private String lv_items[] = { "Android", "iPhone", "BlackBerry",
"AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
"Us", "UK", "India" };
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式在ListView中的CheckedTextView项目上设置"android:checkMark"属性.运行我的应用程序时,我得到以下异常:
android.content.res.Resources$NotFoundException: Resource ID #0x101021a
Run Code Online (Sandbox Code Playgroud)
ID#0x101021a的资源对应于android.R.attr.listChoiceIndicatorMultiple,这正是我传递给CheckedTextView的值:
mCheckedTextView.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple)
Run Code Online (Sandbox Code Playgroud)
这不是用Java做的吗?我尝试(并成功)从XML布局触发所需的行为:
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:id="@android:id/text1" />
Run Code Online (Sandbox Code Playgroud)
问题是我在编译时不知道它应该是什么
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
Run Code Online (Sandbox Code Playgroud)
要么
android:checkMark="?android:attr/listChoiceIndicatorSingle"
Run Code Online (Sandbox Code Playgroud)
因此,我需要在运行时设置这些值.
由于某种原因,setItemChecked不起作用.有人可以帮我解决问题吗?
String[] str = getResources().getStringArray(R.array.brush_type);
sizeArrayAdapter = new ArrayAdapter<String>(this.getContext(), R.layout.drawing_list_item, str);
listType = SIZE_LIST;
listView.setAdapter(sizeArrayAdapter);
// Populate the listView
listView.setItemChecked(4,true);
Run Code Online (Sandbox Code Playgroud)
这是列表项:
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawingCheckedTextView"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:background="@drawable/list_panel"
android:paddingLeft="6dip"
android:paddingRight="6dip"/>
Run Code Online (Sandbox Code Playgroud)
请帮我.
我正在使用ListView,其中一次只能检查一个项目.这是我的自定义list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="20sp"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
/>
Run Code Online (Sandbox Code Playgroud)
我使用普通的数组适配器填充onCreate()中的列表:
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.list_row, strings);
myList.setAdapter(myAdapter);
Run Code Online (Sandbox Code Playgroud)
显示列表时,我想让列表中的第5项显示为已选中.我该怎么做呢?我知道CheckedTextView有一个名为setChecked()的函数,但是如何从列表中获取我的第5项以应用此函数呢?
阅读参考主题属性后,我试图在我设置的自定义主题中引用属性的值.
我将用户定义的样式应用于 CheckedTextView
<CheckedTextView
android:id="@+id/contactInfo"
style="@style/ListViewCheckedTextViewRowStyle" >
</CheckedTextView>
Run Code Online (Sandbox Code Playgroud)
用户定义的样式定义为:
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">?android:listChoiceIndicatorMultiple</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我创建的主题定义为:
<style name="Theme.Yellowgreen" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是,显示的checkMark样式是设备的默认主题的drawable,而不是我的用户定义的drawable.
我可以显示可绘制的唯一方法是:
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但这违背了覆盖此属性的整个目的,特别是因为我想在多个主题中覆盖此属性.
我在onCreate()我的方法中设置主题Activity:
public void onCreate(Bundle savedInstanceState) {
this.setTheme(R.style.Theme_Yellowgreen);
super.onCreate(savedInstanceState);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我还尝试在AndroidManifest.xml文件中设置主题,如:
<application android:theme="@style/Theme.Yellowgreen" >
Run Code Online (Sandbox Code Playgroud)
但那没用.怎么可能出错?
我刚刚创建了一个小型示例项目,看起来我上面发布的代码正在运行.所以我必须有一些其他样式覆盖这个属性,或者它可能与我的布局xml文件有关.
在我的大项目中,我有两个Fragments内Activity.两者Fragments都有Listviews支持Adapters.在Fragment A所述getView()的方法Adapter如下:
public View getView(final int position, View convertView, …Run Code Online (Sandbox Code Playgroud) 我通过扩展SimpleCursorAdapter创建了一个自定义ListView.结果是IMAGE + CheckedTextView(文本+复选框).
当我长按一个项目时,一切正常 - 我得到了正确的ID和被点击的项目的详细信息.
当我尝试将项目标记为已选中但它检查错误的复选框时,会出现此问题.
例如:我的列表中有9个项目,排序为1-9.如果我点击listItem 1,则会检查第9行的复选框.如果我点击第4项,则会检查第6行上的复选框,如果我点击中间一行,则会进行检查.
显然,我在这里遗漏了一些东西:)请记住,当我长时间点击该行(contextMenu打开)时,一切都很好.
这是听众:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView markedItem = (CheckedTextView) view.findViewById(R.id.btitle);
if (!markedItem.isChecked()) {
markedItem.setChecked(true);
} else {
markedItem.setChecked(false);
}
}
});
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助!
如果您需要我发布更多代码,请告诉我.
谢谢!
顺便说一句,如果我点击多个...... PARTY继续......没有明显的顺序......
编辑:适配器代码
public class ImageCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private String url;
private TextView bUrl;
public ImageCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, …Run Code Online (Sandbox Code Playgroud) 来自plattform源的select_dialog_singlechoice_material.xml布局使用android:checkMarkGravity ="start"属性:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the …Run Code Online (Sandbox Code Playgroud) 我实现的布局如下图所示:
我使用的代码是:
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/big_green_btn_normal"
android:checkMark="@drawable/check_circle"
android:gravity="center_vertical"
android:text="@string/register_tempClickStaffingAgreement" />
Run Code Online (Sandbox Code Playgroud)
这就是我想要的,如下图所示:
分享一些想法来定制它,但不是自定义线性或任何其他布局..我想要一个自定义视图或checkedTextView ...任何帮助将不胜感激.
谢谢..!!
android ×10
checkedtextview ×10
listview ×4
checkbox ×2
listviewitem ×1
overriding ×1
styles ×1
textview ×1
themes ×1