我想在点击EditText
之后更改final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(text1 + text2);
editText.setSelection(text1.length());
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setSelection(editText.getText().length() - text2.length());
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!editable.toString().contains(text1) || !editable.toString().contains(text2)) {
editText.setText(text1 + text2);
editText.setSelection(text1.length());
}
}
});
}
中的光标位置
我希望点击后将位置光标更改为十到十二点之间,点击第一次。
这是我的代码:
public Organization findAndModifyOrganization(Integer orgId, String name, Integer empId)
{
Query query = new Query();
query.addCriteria(Criteria.where("employees.id").is(empId));
query.addCriteria(Criteria.where("id").is(orgId));
Update update = new Update();
update.set("employees.$.name", name);
Organization org = mongoTemplate.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Organization.class);
return org;
}
答案 0 :(得分:1)
如果我保持简单,下面的代码就足以实现你想要的(显然)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<table>
<tr id="row">
<td> This is td </td>
<td> This is td2 </td>
</tr>
</table>
<div id="subpage"> </div>
如果它是第一个获得焦点的EditText,上面的代码可以正常工作,否则,如果软键盘已经打开,则首先在轻敲的位置显示光标。这是解决这个问题的方法(不是最好的解决方案,但它会对你有用)
final EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(text1 + text2);
// Keep it simple for the selection, it will place the cursor at end of text1
editText.setSelection(text1.length());
// editText.setCursorVisible(true); // I don't think it is required
// You don't need to go for OnTouchListener, OnClickListener will do for you
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setSelection(editText.getText().length() - text2.length());
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!editable.toString().contains(text1) || !editable.toString().contains(text2)) {
editText.setText(text1 + text2);
editText.setSelection(text1.length());
}
}
});
答案 1 :(得分:0)
使用应该使用android:imeOptions =&#34; actionNext&#34;在你的xml EditText中。这对你有帮助。
答案 2 :(得分:0)
我不确定请尝试
editText.setText(text1 + text2);
editText.setSelection(editText.getText().length() - text2.length());
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
(EditText)view.setSelection((EditText)view.getText().length() - text2.length());
return false;
}
});
答案 3 :(得分:0)
而不是Selection.setSelection使用editText.setSelection()