我有一个有碎片的活动..。在这个片段中,我在一个DataBinging中使用了双向EditText。此EditText绑定到对象的双重属性,因此,我不得不实现一个InverseMethod来转换字符串->双字符串和双->字符串.
在我的android:selectAllOnFocus="true",中,我配置了edQtd.selectAll(),并且强制使用片段的onCreateView方法:edQtd.selectAll()
问题是,当片段出现时,EditText有焦点,但是没有选择文本,相反,光标位于第一个数字之前.
我想让它显示所有选定的文字..。
尝试不使用逆方法,只是连接一个空字符串,但结果是相同的.
根据我所看到的破坏它的情况,生成的绑定类在片段创建后设置文本(在我手动调用edQtd.selectAll()),删除所选内容之后)。
有什么办法解决吗?
编辑:,现在我解决了它向EditText中添加一个TextChangedListener,其中只在第一次更改文本时选择所有文本:
edQuantidade.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(selectAllEdQtdText) {
edQuantidade.selectAll();
setSelectAllEdQtdText(false);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});发布于 2018-11-20 13:54:26
在布局中在EditText上添加以下属性。
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:selectAllOnFocus="true"
/>并从代码中删除edQtd.selectAll()。
编辑
因为没有解决方案有效。这将起作用,因为这将触发延迟后的selectAll。在绑定中设置模型后添加此选项。
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
edQtd.selectAll();
}
}, 500);https://stackoverflow.com/questions/53393598
复制相似问题