当我的片段开始时,我希望我的edittext成为焦点/让用户开始输入它。我能够通过requestFocus()获得焦点,但我无法显示键盘。
我试过这两个:
edit = (EditText) view.findViewById(R.id.search);
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
和
edit = (EditText) view.findViewById(R.id.search);
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
edit.requestFocus();
如何让键盘显示EditText?
答案 0 :(得分:87)
这有用吗?
imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
答案 1 :(得分:10)
你可以试试这个
@Override
public void onResume() {
super.onResume();
edit.post(new Runnable() {
@Override
public void run() {
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
}
});
}
答案 2 :(得分:7)
由于使用showSoftInput
并不适用于所有情况,并且在尝试了此处提到的某些解决方案之后,例如:
if (binding.account.requestFocus()) {
getActivity().getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
我终于使用:
了if (binding.account.requestFocus()) {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY
);
}
自:
binding.account.requestFocus()
仅请求EditText
的焦点(它不会打开键盘)
和
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY
);
是唯一能够正常显示键盘(以及投票最多的键盘)的解决方案
祝你好运! : - )答案 3 :(得分:5)
我对此有帮助的扩展名:
fun EditText.showKeyboard() {
if (requestFocus()) {
(getActivity()?.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(this, SHOW_IMPLICIT)
setSelection(text.length)
}
}
您还将需要这个:
fun View.getActivity(): AppCompatActivity?{
var context = this.context
while (context is ContextWrapper) {
if (context is AppCompatActivity) {
return context
}
context = context.baseContext
}
return null
}
答案 4 :(得分:2)
@Override
public void onHiddenChanged (boolean hidden)
{
super.onHiddenChanged(hidden);
if(hidden)
{
hideKeyboard(yourView);
}
else
{
toggleKeyboard(yourView);
}
}
public static void toggleKeyboard(View v)
{
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
v.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void hideKeyboard(View v)
{
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
答案 5 :(得分:1)
在尝试了所有此处的解决方案以及其他相关问题之后,以下是适用于我的方法:
editText.postDelayed(Runnable { showKeyboard(activity, editText)} , 50)
fun showKeyboard(activity: Activity, editText: EditText) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
editText.requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
有趣的事实是,当您不带postDeleayed调用它时,它将不起作用,即使您只是将其延迟1毫秒也仍然无法起作用:D
您也可以将其用作扩展程序,如下所示:
fun EditText.showKeyboard(activity: Activity) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
如果您不希望将活动作为参数传递,请按照@Rafols的建议使用此扩展功能:
fun View.getActivity(): AppCompatActivity? {
var context = this.context
while (context is ContextWrapper) {
if (context is AppCompatActivity) {
return context
}
context = context.baseContext
}
return null
}
然后您的方法将如下所示:
fun EditText.showKeyboard() {
val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
}
如果您的EditText中已经有文本,则最好在现有文本的末尾进行设置:
fun EditText.showKeyboard() {
val inputMethodManager = getActivity()!!.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
requestFocus()
inputMethodManager.showSoftInput(this, 0)
setSelection(length())
}
如果要在片段开始时启动它:
override fun onResume() {
super.onResume()
editText.postDelayed(Runnable { editText.showKeyboard()} , 50)
}
答案 6 :(得分:1)
科特林
要自动显示/隐藏片段中的键盘...
// Display 2 select fields (car brands and car models)
add_action( 'woocommerce_before_shop_loop', 'before_shop_loop_action_callback', 3 );
function before_shop_loop_action_callback() {
if( $attributes = wc_get_attribute_taxonomies() ) {
## 1st dropdown
echo '<select id="car-brands" style="min-width:100px;"><option value="">' . __("Car Brand"). '</option>';
// Loop through attribute taxonomies
foreach ( $attributes as $attribute ) {
echo '<option value="' . $attribute->attribute_name . '">' . $attribute->attribute_label . '</option>';
}
echo '</select>';
## 2nd dropdown
echo '<select id="car-models" style="min-width:100px;"><option value=""> … </option></select>';
}
}
// jQuery / Ajax (client side)
add_action( 'wp_footer', 'car_brand_selectors_script' );
function car_brand_selectors_script() {
?>
<script type="text/javascript">
jQuery(function( $ ) {
if (typeof woocommerce_params === 'undefined')
return false;
var b = 'select#car-brands', // 1st field
m = 'select#car-models', // 2nd field
r = $(m).html(); // Original 2nd field select options
function ajaxSendCarBrand( carBrand ) {
$.ajax({
url: woocommerce_params.ajax_url,
type : 'POST',
data : {
'action' : 'get_brand_terms',
'car_brand' : carBrand
},
success: function( response ) {
var options = $.parseJSON(response),
opt = '';
if ( $.isEmptyObject(options) ) {
$(m).html(r);
} else {
$.each( options, function( key, value ){
opt += '<option value="'+key+'">'+value+'</option>';
});
$(m).html(opt);
}
}
});
}
// On change live event
$( document.body ).on( 'change', b, function() {
ajaxSendCarBrand($(this).val());
});
});
</script>
<?php
}
// WP AJAX HANDLER (Server side)
add_action('wp_ajax_get_brand_terms', 'get_car_brand_models');
add_action('wp_ajax_nopriv_get_brand_terms','get_car_brand_models');
function get_car_brand_models() {
if( isset($_POST['car_brand']) ) {
$brand = wc_clean( $_POST['car_brand'] );
$taxonomy = wc_attribute_taxonomy_name($brand);
$options = [];
if( taxonomy_exists( $taxonomy ) ) {
$terms = get_terms( array( 'taxonomy' => $taxonomy ) );
foreach( $terms as $term ){
$options[$term->slug] = $term->name;
}
}
echo json_encode( $options );
}
wp_die();
}
少量信息here。
答案 7 :(得分:0)
在片段开始时让键盘处于打开状态的另一种方法是在requestFocus()
中调用onCreateView
并通过打开键盘做出相应的反应,当且仅当EditText
可以调焦时。
if(this.editText.requestFocus())
{
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
答案 8 :(得分:0)
EditText。 requestFocus ()+ InputMethodManager。 showSoftInput ()=显示用于EditText的IME
使用Fragment.onViewCreated()中的EditText。 performAccessibilityAction (AccessibilityNodeInfo.ACTION_CLICK,空)代替
void maybeShowInputMethod() {
// use addOnPreDrawListener instead of addOnGlobalLayoutListener
// because execute sequence: onGlobalLayout-> Restore focus -> onPreDraw
getView().getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
getView().getViewTreeObserver().removeOnPreDrawListener(this);
// always requestFocus when fragment enter or show
getView().requestFocus();
final View currentFocus = getView().findFocus();
if ((currentFocus != null) && currentFocus.onCheckIsTextEditor()) {
Log.d(TAG, "maybeShowInputMethod:: currentFocus=" + currentFocus);
currentFocus.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null);
}
return true;
}
});
}
或创建EditText的子类并覆盖 public InputConnection onCreateInputConnection(EditorInfo editorInfo)
public class ImeAwareEditText extends EditText {
private boolean mHasPendingShowSoftInputRequest;
final Runnable mRunShowSoftInputIfNecessary = () -> showSoftInputIfNecessary();
public ImeAwareEditText(Context context) {
super(context, null);
}
public ImeAwareEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ImeAwareEditText(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
* This method is called back by the system when the system is about to establish a connection
* to the current input method.
*
* <p>This is a good and reliable signal to schedule a pending task to call
52,6 Top
*
* <p>This is a good and reliable signal to schedule a pending task to call
* {@link InputMethodManager#showSoftInput(View, int)}.</p>
*
* @param editorInfo context about the text input field.
* @return {@link InputConnection} to be passed to the input method.
*/
@Override
public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
final InputConnection ic = super.onCreateInputConnection(editorInfo);
if (mHasPendingShowSoftInputRequest) {
removeCallbacks(mRunShowSoftInputIfNecessary);
post(mRunShowSoftInputIfNecessary);
}
return ic;
}
private void showSoftInputIfNecessary() {
if (mHasPendingShowSoftInputRequest) {
final InputMethodManager imm =
getContext().getSystemService(InputMethodManager.class);
imm.showSoftInput(this, 0);
mHasPendingShowSoftInputRequest = false;
}
}
public void scheduleShowSoftInput() {
final InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
if (imm.isActive(this)) {
// This means that ImeAwareEditText is already connected to the IME.
// InputMethodManager#showSoftInput() is guaranteed to pass client-side focus check.
mHasPendingShowSoftInputRequest = false;
removeCallbacks(mRunShowSoftInputIfNecessary);
imm.showSoftInput(this, 0);
return;
}
// Otherwise, InputMethodManager#showSoftInput() should be deferred after
// onCreateInputConnection().
mHasPendingShowSoftInputRequest = true;
}
}
答案 9 :(得分:0)
科特琳
启动片段时键盘会打开
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val fragmentView = inflater.inflate(R.layout.fragment_main, container, false)
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return fragmentView
}
答案 10 :(得分:-1)
正如Nilzor说的那样有效
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT)
我同意这是一个比toogleSoftInput
更好的解决方案答案 11 :(得分:-1)
这篇文章帮助了我
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_edit_name, container);
editText = (EditText) view.findViewById(R.id.txt_yourName);
editText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return view;
}
答案 12 :(得分:-1)
简单地说,使用添加2行就像魅力一样:
如果使用XML
android:focusable="true"
android:focusableInTouchMode="true"
其他Java:
view.setFocusableInTouchMode(true);
view.requestFocus();