java-如何在Android中以编程方式添加焦点?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在Android中以编程方式添加焦点?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2582字,纯文字阅读大概需要4分钟。
内容图文
我有两个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 {
@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>
但是主要的问题是您的活动生命周期中重写了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);
}
}
内容总结
以上是互联网集市为您收集整理的java-如何在Android中以编程方式添加焦点?全部内容,希望文章能够帮你解决java-如何在Android中以编程方式添加焦点?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。