如何在Android中通过编程方式添加焦点?

3

我有两个EditText视图(用户名和密码),如果两个字段都为空,我希望聚焦在第一个edittext上,如果已经输入了用户名,则应该将焦点放在密码字段上。到目前为止,我尝试了所有解决方案,但似乎没有任何作用。

edtPassword.requestFocus(); 
edtPassword.setFocusableInTouchMode(true);
edtPassword.getParent().requestChildFocus(edtPassword,edtPassword);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

碎片类:

public class ReloginpopupFragment extends DialogFragment {



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, R.style.DialogTheme);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.popup_relogin, container);
    unbinder = ButterKnife.bind(this, view);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (sharedPreferenceManager.getCurrentUser().getUserID() != null) {
        edtUserName.setText(sharedPreferenceManager.getCurrentUser().getUserID());
        edtPassword.requestFocus();

    }
    setListener();
    PackageManager packageManager = getContext().getApplicationContext().getPackageManager();
    String packageName = getContext().getApplicationContext().getPackageName();
    try {
        String myVersionName = packageManager.getPackageInfo(packageName, 0).versionName; txtAppVersion.setText("App Version "+myVersionName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

}

}

2个回答

2

您可以使用以下方法聚焦于editText字段:

通过编程方式:

edittext.requestFocus();

通过XML:

<EditText...>
    <requestFocus />
enter code here
</EditText>

但主要问题在于您的活动生命周期,其中覆盖了onResume方法。
@Override
public void onResume() {
    super.onResume();
    KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
}

您可以将逻辑从onViewCreated()替换为onResume()

  @Override
    public void onResume () {
        super.onResume();
        if (sharedPreferenceManager.getCurrentUser().getUserID() != null) {

            edtUserName.setText(sharedPreferenceManager.getCurrentUser().getUserID());
            edtPassword.requestFocus();
            KeyboardHelper.showSoftKeyboard(getContext(), edtPassword);

        } else {
            KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
        }

    }

1
EditText editText = (EditText) findViewById(R.id.myTextViewId);


editText.requestFocus();


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

1
最好在代码中包含一些上下文或解释。 - ADM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接