我有两个EditText视图(Username和Password),如果两个字段都为空,我想集中在第一个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 {@OverrIDepublic voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(STYLE_NO_Title, R.style.Dialogtheme);}@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getDialog().getwindow().setSoftinputMode(WindowManager.LayoutParams.soFT_input_STATE_VISIBLE);}@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.popup_relogin, container); unbinder = ButterKnife.bind(this, vIEw); return vIEw;}@OverrIDepublic voID onResume() { super.onResume(); KeyboardHelper.showSoftKeyboard(getContext(), edtUsername);}@OverrIDepublic 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>
但是主要的问题是您的活动生命周期中重写了onResume方法
@OverrIDepublic 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); } }
总结 以上是内存溢出为你收集整理的java-如何在Android中以编程方式添加焦点?全部内容,希望文章能够帮你解决java-如何在Android中以编程方式添加焦点?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)