Hou*_*ine 159 android focus android-edittext
我有一个布局,其中包含一些像这样的视图:
<LinearLayout>
<TextView...>
<TextView...>
<ImageView ...>
<EditText...>
<Button...>
</linearLayout>
Run Code Online (Sandbox Code Playgroud)
如何以EditText编程方式设置焦点(显示键盘)?
我已经尝试了这个,只有当我Activity正常启动时它才有效,但是当我在a中启动它时TabHost,它不起作用.
txtSearch.setFocusableInTouchMode(true);
txtSearch.setFocusable(true);
txtSearch.requestFocus();
Run Code Online (Sandbox Code Playgroud)
Dav*_*man 315
试试这个:
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)
http://developer.android.com/reference/android/view/View.html#requestFocus()
ung*_*rys 159
使用:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
Dan*_*spa 49
显示键盘:
editText = (EditText)findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
隐藏键盘:
InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
vin*_*odi 40
showSoftInput 根本不适合我.
我想我需要设置输入模式:( android:windowSoftInputMode="stateVisible"此处在清单中的Activity组件中)
希望这有帮助!
小智 34
final EditText tb = new EditText(this);
tb.requestFocus();
tb.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(tb, InputMethodManager.SHOW_IMPLICIT);
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
alv*_*lof 12
以下是如何使用kotlin扩展来显示和隐藏软键盘:
fun View.showKeyboard() {
this.requestFocus()
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
editText.showKeyboard()
// OR
editText.hideKeyboard()
Run Code Online (Sandbox Code Playgroud)
这是用于隐藏和显示键盘的 KeyboardHelper 类
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(final Context context, final View view) {
if (context == null) {
return;
}
view.requestFocus();
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}, 1000);
}
public static void hideSoftKeyboard(final Context context, final EditText editText) {
editText.requestFocus();
editText.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}, 1000);
}
public static void openSoftKeyboard(final Context context, final EditText editText) {
editText.requestFocus();
editText.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
我建议使用LifecycleObserver这是一部分具有生命周期感知组件处理生命周期的安卓Jetpack的。
我想在片段/活动出现时打开和关闭键盘。首先,为 EditText定义两个扩展函数。您可以将它们放在项目中的任何位置:
fun EditText.showKeyboard() {
requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun EditText.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
Run Code Online (Sandbox Code Playgroud)
然后定义一个 LifecycleObserver,它在 Activity/Fragment 到达onResume()或时打开和关闭键盘onPause:
class EditTextKeyboardLifecycleObserver(private val editText: WeakReference<EditText>) :
LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun openKeyboard() {
editText.get()?.postDelayed({ editText.get()?.showKeyboard() }, 100)
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun closeKeyboard() {
editText.get()?.hideKeyboard()
}
}
Run Code Online (Sandbox Code Playgroud)
然后将以下行添加到您的任何片段/活动中,您可以随时重用 LifecycleObserver。例如对于片段:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// inflate the Fragment layout
lifecycle.addObserver(EditTextKeyboardLifecycleObserver(WeakReference(myEditText)))
// do other stuff and return the view
}
Run Code Online (Sandbox Code Playgroud)
小智 6
将其放入 onResume() 方法中。
binding.etxtSearch.isFocusableInTouchMode = true
binding.etxtSearch.isFocusable = true
binding.etxtSearch.requestFocus()
val inputMethodManager = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(binding.etxtSearch, InputMethodManager.SHOW_IMPLICIT)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
207053 次 |
| 最近记录: |