最近有个项目是没有触摸的,外接的16键硬件键盘,但EditText在获取焦点时还是会弹出输入法。
不想对每个EditText都设置监听处理。有没有更优雅的方法能让程序在运行期间都不会弹出输入法?最好是只要配置一下AndroidManifest.xml或者在Application里面运行一段代码。
伊谢尔伦2017-04-18 09:09:34
The global theme style used in your AndroidManifest.xml
should look like this:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">//使用的是AppTheme主题样式
Then what you need to do is, in the AppTheme
中设置 EditText
style, the complete styles.xml is as follows:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="editTextStyle">@style/NoFocusableEditText</item>
</style>
<style name="NoFocusableEditText" parent="Widget.AppCompat.EditText">
<item name="android:focusable">false</item>//没有焦点就不会弹出了
</style>
In this way, the EditText of the entire App is set.
/-----------------------Since the questioner added a description based on the question, I updated the answer as follows---------- ---------------/
Add the following code in the onCreate
function of your Activity to completely disable the soft keyboard:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
So, if you want it to take effect globally, you can write a base class, for example:
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);
}
Then any Activity inherits it. If your project already has a base class, just insert the above code into the base class. Demo example:
public class TestActivity extends BaseNoIMActivity {//继承基类
@Override
protected void onCreateStarted(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_test);
Log.e("test","TestActivity is started");
}
}