本文介绍了无法解析符号 showsoftinput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试强制数字键盘显示我的活动和 EditText 何时加载.这里和其他地方:你说

I'm trying to force the numeric keypad to show when my activity and EditText load. It seems there's a pretty straightforward answer given here and elsewhere: you say

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

那好吧.所以我这样做并包含导入:

Fine then. So I do this and I include the imports:

import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

但是当我们到达 showSoftInput 时,Studio 变红并显示无法解析符号 showSoftInput".当我导入 InputMethodManager 时,它不应该得到那个符号吗?showSoftInput 似乎没有被弃用之类的.

But when we get to showSoftInput, Studio goes red and says "Cannot resolve symbol showSoftInput". Shouldn't it be getting that symbol when I import the InputMethodManager? showSoftInput doesn't seem to be deprecated or anything.

推荐答案

键盘打不开,因为它需要一些延迟,

Keyboard is not opening because it needs some delay ,

关注此代码

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
       /* */

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            editText.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        }
    },100);
    }
}

和 XML

 <EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:focusable="true"
    android:inputType="number"
    android:text="">

    <requestFocus></requestFocus>
</EditText>

这篇关于无法解析符号 showsoftinput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 23:17