安卓应用如何在整个应用运行期间都禁止弹出输入法面板?

在你的Activity的onCreate函数中添加如下代码即可完全禁用软键盘:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

所以,你想全局生效的话,可以写一个基类,例如:

public abstract class BaseNoIMActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        onCreateStarted(savedInstanceState);
    }

    protected abstract void onCreateStarted(@Nullable Bundle savedInstanceState);
}

然后任意个Activity都继承它,如果你的项目已经有基类,那么把上面的代码插入到基类中即可,demo示例:

public class TestActivity extends BaseNoIMActivity {//继承基类

    @Override
    protected void onCreateStarted(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_test);
        Log.e("test","TestActivity is started");
    }
}

 

引用自:https://segmentfault.com/q/1010000008489927/a-1020000008490261/revision

posted @ 2017-11-21 18:31  _Vincent  阅读(1982)  评论(0编辑  收藏  举报