gpt4 book ai didi

java - 单击时返回整个字符串的 Spannable 字符串

转载 作者:行者123 更新时间:2023-11-30 01:01:28 26 4
gpt4 key购买 nike

ListView 中的一行上使用 Button 并且能够使单行 Spannable 和数据库中的数据但是当我单击它时返回整个字符串而不是一个单词这里是我的代码来自 MyAdapter 类 将数据库中的值输入到 Listview

public View getView(int i, View view, ViewGroup viewGroup) {
Realm realm=Realm.getDefaultInstance();
Word toEdit = realm.where(Word.class)
.equalTo("id", 10).findFirst();
int id_to_seperate=toEdit.getLang();

LayoutInflater inflater= (LayoutInflater) Main.context.getSystemService(Main.context.LAYOUT_INFLATER_SERVICE);
View row= inflater.inflate(R.layout.layout,viewGroup,false);
TextView word= (TextView) row.findViewById(R.id.word_name);
TextView meaning= (TextView) row.findViewById(R.id.word_define);
Word temp=list.get(i);
int idz=temp.getId();
word.setText(temp.getWord());
if(id_to_seperate==idz){
String span[] = temp.getMeaning().substring(1).split(" ") ;
SpannableString ss = new SpannableString(temp.getMeaning().substring(1));
ClickableSpan spans[] = new ClickableSpan[span.length];
for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new ClickableSpan() {
@Override
public void onClick(View textView) {

TextView v = (TextView)textView ;
String text = v.getText().toString() ;

Log.d("View" , text);
}
};
}

int start = 0 ;
int end =0;
try {
for(int spanCount = 0 ; spanCount <span.length ; spanCount++){
if(spanCount==0) {
end += span[spanCount].length();
}else{

end += span[spanCount].length()+1;
}
ss.setSpan(spans[spanCount], start, end , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += span[spanCount].length()+1;

}
} catch (Exception e) {
e.printStackTrace();
}

meaning.setText(ss);
meaning.setMovementMethod(LinkMovementMethod.getInstance());

}else {
meaning.setText(temp.getMeaning().substring(1));
}
return row;
}

我能够从列表中的特定行获取结果 enter image description here
对此enter image description here但是点击功能返回整个句子而不是一个跨度或单词,如果有人能告诉我我犯了什么错误,我将不胜感激
问候

最佳答案

你的代码有问题

TextView v = (TextView)textView ;
String text = v.getText().toString() ;

TextView.getText().toString() 是返回全文。如果你想获得特定的 ClickableSpan 然后使用下面的 ClickableSpan

public class SpecialClickableSpan extends ClickableSpan {

String text;

public SpecialClickableSpan(String text){
super();
this.text = text;
}

@Override
public void onClick(View widget) {
Log.d(TAG, "onClick [" + text + "]");
}
}

替换

for(int spanCount = 0 ; spanCount < span.length ; spanCount++){

spans[spanCount] = new ClickableSpan() {
@Override
public void onClick(View textView) {

TextView v = (TextView)textView ;
String text = v.getText().toString() ;
Log.d("View" , text);
}
};
}

for(int spanCount = 0 ; spanCount < span.length ; spanCount++){
spans[spanCount] = new SpecialClickableSpan (span[spanCount ]);
}

关于java - 单击时返回整个字符串的 Spannable 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com