[英]How to set the cursor to the editText
I have two EditText, how can I set the cursor in the right EditText 我有两个EditText,如何在右侧的EditText中设置光标
EditText emailE = (EditText) findViewById(R.id.editTextEmailLogin);
EditText passwordE = (EditText)findViewById(R.id.editTextPasswordLogin);
String email = emailE.getText().toString().trim();
String password = passwordE.getText().toString().trim();
if the user pressing Singin and the email EditText is empty, set the cursor in the email EditText. 如果用户按下Singin并且电子邮件EditText为空,请将光标设置在电子邮件EditText中。 and the same for password EditText
密码EditText也一样
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.setSelection(0);
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.setSelection(0);
return;
}
Try using the .requestFocus();
尝试使用
.requestFocus();
method 方法
Try this: 尝试这个:
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.requestFocus();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.requestFocus();
return;
}
you can use requestFocus(); 你可以使用requestFocus(); method of edittext like this
像这样的edittext的方法
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.requestFocus();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.requestFocus();
return;
}
etext1.setSelection(Your position)
or 要么
EditText etext1 = (EditText)findViewById(R.id.etext1 );
etext1.setSelection(etext1.getText().length());
or 要么
etext1 .requestFocus(Your_Text.length());
Try this one also; 也尝试这个; Check this
检查一下
EditText editText = (EditText) findViewById(R.id.myTextViewId);
if (TextUtils.isEmpty(email)){
Toast.makeText(this, getResources()"email is empty", Toast.LENGTH_SHORT).show();
//set cursor in Email editText
emailE.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(emailE, InputMethodManager.SHOW_IMPLICIT);
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"password is empty", Toast.LENGTH_SHORT).show();
//set cursor in Password editText
passwordE.requestFocus();
InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm2.showSoftInput(passwordE, InputMethodManager.SHOW_IMPLICIT);
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.