TextView中的setText不会对custome布局preferenceFragment工作 [英] TextView setText doesn't work on custome layout in PreferenceFragment
问题描述
我用 preferenceFragment
一些默认preferences类别和一个自定义布局:
I'm using PreferenceFragment
with some default preferences categories and one custom layout:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="first_category"
android:title="config" >
<EditTextPreference
android:defaultValue="example"
android:dialogMessage="text"
android:dialogTitle="Title"
android:key="mykey"
android:summary="summary"
android:title="title" />
<Preference
android:key="test_connection"
android:title="Test connection"
android:layout="@layout/test_conn" />
</PreferenceCategory>
</PreferenceScreen>
我test_conn布局:
My test_conn layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:text="Test connection"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:text="Ko"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ff0000"
android:textStyle="bold" />
</RelativeLayout>
我在看的TextView R.id.summary
这个code:
addPreferencesFromResource(R.xml.preferences);
findPreference(TEST_CONN).setOnPreferenceClickListener(this);
Preference custom = findPreference(TEST_CONN);
customv = custom.getView(null, null);
res = (TextView) customv.findViewById(R.id.summary);
我能正确地读我的TextView的,但如果我尝试改变文字,例如AsyncTask的里面,我无法看到的文本改变了。
I can read correctly my TextView, but if I try to change the text, for example inside an asynctask, I can't see the text changed.
这是AsyncTask的一部分:
This is a part of asynctask:
@Override
protected void onPostExecute(Boolean result) {
if (result == true){
res.setText("Ok");
res.setTextColor(Color.GREEN);
}
else {
res.setText("Ko");
res.setTextColor(Color.RED);
}
pdia.dismiss();
}
在上preferencetest_connection,一开始的AsyncTask和onPostExecute我修改文本,但不工作的funzion用户点击。我缺少的东西吗?我应该读与其他方法TextView的?
When the user click on the preference "test_connection", an asynctask starts and in the funzion onPostExecute I modify the text, but doesn't work. Am I missing something? Should I read the textview with another method?
推荐答案
下面是一个解决方案:您需要实现扩展了Android定制preference类 preference
类。 请确保您的自定义类是在一个单独的文件 - 不是另一个类的一部分 - 否则,你会得到一个充气XML错误'像<一个href=\"http://stackoverflow.com/questions/23424040/android-view-inflateexception-binary-xml-file-line-10-error-inflating-class-f\">this.这里有一个例子:
Here is a solution: You need to implement a custom Preference class that extends the Android Preference
class. Ensure that your custom class is in a separate file -- not part of another class -- otherwise you will get an 'Inflate XML error' like this. Here's an example:
我的自定义preference.java 文件:
public class CustomPreference extends Preference {
private TextView txt;
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWidgetLayoutResource(R.layout.test_conn);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
txt = (TextView) view.findViewById(R.id.summary);
}
public void setText(String text, int color) {
txt.setText(text);
txt.setTextColor(color);
}
}
您的的settings.xml 文件应该插入/使用自定义preference类是这样的:
Your settings.xml file should insert/use the custom Preference class like this:
<com.myapp.example.widget.CustomPreference
android:name="com.myapp.example.widget.CustomPreference"
android:key="test_connection"
android:title="Test connection"
android:layout="@layout/test_conn" />
当您要修改的TextView的或按钮上的自定义布局,您需要调用public方法的setText()
,提供如文本和颜色参数:
When you want to modify the TextView or Button on your custom layout, you need to call the public method setText()
, providing parameters like text and color:
@Override
protected void onPostExecute(Boolean result) {
if (result == true){
CustomPreference custom = (CustomPreference) findPreference(TEST_CONN);
custom.setText("Ok", Color.GREEN);
}
else {
CustomPreference custom = (CustomPreference) findPreference(TEST_CONN);
custom.setText("Ko", Color.RED);
}
pdia.dismiss();
}
另请参阅:这里另一个答案
这篇关于TextView中的setText不会对custome布局preferenceFragment工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!