Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中以编程方式添加焦点?_Java_Android - Fatal编程技术网

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

Java 如何在Android中以编程方式添加焦点?,java,android,Java,Android,我有两个EditText视图(用户名和密码),如果两个字段都是空的,我想将焦点放在第一个EditText上,如果用户名已经输入,则应该将焦点放在密码字段上。到目前为止,我已经尝试了所有的解决方案,但似乎没有任何效果 edtPassword.requestFocus(); edtPassword.setFocusableInTouchMode(true); edtPassword.getParent().requestChildFocus(edtPassword,edtPassword); ge

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

}

}

您可以通过这些方法关注editText字段

在程序上:

edittext.requestFocus();
通过xml:

<EditText...>
    <requestFocus />
enter code here
</EditText>
您可以将逻辑从onViewCreated()替换为onResume()


最好在代码中包含一些上下文或解释。
<EditText...>
    <requestFocus />
enter code here
</EditText>
@Override
public void onResume() {
    super.onResume();
    KeyboardHelper.showSoftKeyboard(getContext(), edtUserName);
}
  @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);
        }

    }