android 工作室更改自动完成文本视图数组适配器中部分字符串的字体颜色
android studio change font color of part of string in autocompletetextview arrayadapter
您将在下面的代码中看到我连接了 2 个字符串(strName 和 StrSciName)并将结果添加到一个数组列表中,该数组列表被添加到一个数组适配器中以显示在自动完成文本视图中。
我的问题是:我希望能够在自动完成文本视图中以斜体显示部分文本。所以我希望 strSciName ( [ ] 中的部分)以斜体显示。
这可能吗?
谢谢
final ArrayList<String> arrForAdapter = new ArrayList<String>();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
String strName = jsonObject.getString("common_name");
String strSciName = jsonObject.getString("scientific_name");
String strVal = strName + " [ " + strSciName + " ]";
arrForAdapter.add(strVal);
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(actAddSighting.this, android.R.layout.simple_list_item_1, arrForAdapter);
atvAnimalName.setAdapter(adapter);
1) 通过 Spannable
String strVal = strName + " [ " + strSciName + " ]";
Spannable spannable = new SpannableString(strVal);
spannable.setSpan(new ForegroundColorSpan(Color.WHITE), strName.length()+1, (strName+ strSciName).length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//Setting value on textview
myTextView.setText(spannable, TextView.BufferType.SPANNABLE);
NOTE: '+1' is included in start and end to include your brackets, it
can be modified as per your need.
2) 通过HTML
//Setting value on textview
myTextView.setText(Html.fromHtml("<font color=\"#FFFFFF\">" +YOUR TEXT));
UPDATE: Seems like you have changed your question. As per change you
can try below code
SpannableString spannableString = SpannableString("String which needs in italic")
SpannableStringBuilder spannableBuilder = SpannableStringBuilder()
spannableBuilder.append("OTHER STRING TO CONCAT")
spannableBuilder.append(spannableString, StyleSpan(Typeface.ITALIC), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
myTextView.setText(spannableBuilder,TextView.BufferType.SPANNABLE)
您将在下面的代码中看到我连接了 2 个字符串(strName 和 StrSciName)并将结果添加到一个数组列表中,该数组列表被添加到一个数组适配器中以显示在自动完成文本视图中。
我的问题是:我希望能够在自动完成文本视图中以斜体显示部分文本。所以我希望 strSciName ( [ ] 中的部分)以斜体显示。
这可能吗?
谢谢
final ArrayList<String> arrForAdapter = new ArrayList<String>();
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
String strName = jsonObject.getString("common_name");
String strSciName = jsonObject.getString("scientific_name");
String strVal = strName + " [ " + strSciName + " ]";
arrForAdapter.add(strVal);
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(actAddSighting.this, android.R.layout.simple_list_item_1, arrForAdapter);
atvAnimalName.setAdapter(adapter);
1) 通过 Spannable
String strVal = strName + " [ " + strSciName + " ]";
Spannable spannable = new SpannableString(strVal);
spannable.setSpan(new ForegroundColorSpan(Color.WHITE), strName.length()+1, (strName+ strSciName).length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//Setting value on textview
myTextView.setText(spannable, TextView.BufferType.SPANNABLE);
NOTE: '+1' is included in start and end to include your brackets, it can be modified as per your need.
2) 通过HTML
//Setting value on textview
myTextView.setText(Html.fromHtml("<font color=\"#FFFFFF\">" +YOUR TEXT));
UPDATE: Seems like you have changed your question. As per change you can try below code
SpannableString spannableString = SpannableString("String which needs in italic")
SpannableStringBuilder spannableBuilder = SpannableStringBuilder()
spannableBuilder.append("OTHER STRING TO CONCAT")
spannableBuilder.append(spannableString, StyleSpan(Typeface.ITALIC), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
myTextView.setText(spannableBuilder,TextView.BufferType.SPANNABLE)