我有一个包含如下视图的布局:
<LinearLayout>
<TextView...>
<TextView...>
<ImageView ...>
<EditText...>
<Button...>
</linearLayout>
如何以编程方式在我的EditText上设置焦点(显示键盘)?
我已经尝试过了,它只在我正常启动我的活动时才起作用,但当我在TabHost中启动它时,它不起作用。
txtSearch.setFocusableInTouchMode(true);
txtSearch.setFocusable(true);
txtSearch.requestFocus();
这里是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);
}
}