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

时间:2019-01-15 12:04:12

标签: java android

我有两个EditText视图(用户名和密码),如果两个字段都为空,我想集中在第一个edittext上,如果已经输入用户名,则应该集中在Password字段上。到目前为止,我已经尝试了所有解决方案,但似乎无济于事。

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 个答案:

答案 0 :(得分: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 :(得分:1)

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


editText.requestFocus();


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

imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);