如何将光标设置为editText

时间:2017-06-20 10:16:11

标签: java android

我有两个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();

如果用户按下Singin并且电子邮件EditText为空,请将光标设置在电子邮件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;
    }

5 个答案:

答案 0 :(得分:2)

尝试使用.requestFocus();方法

答案 1 :(得分:2)

试试这个:

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;
    }

答案 2 :(得分:2)

您可以使用 requestFocus(); 这样的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;
}

答案 3 :(得分:2)

etext1.setSelection(Your position)

 EditText etext1 = (EditText)findViewById(R.id.etext1 );
    etext1.setSelection(etext1.getText().length());  

etext1 .requestFocus(Your_Text.length());

也尝试这个; Check this

答案 4 :(得分:1)

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;
}