【发布时间】:2015-01-07 23:58:31
【问题描述】:
我有一个在打开键盘时已经打开的活动,因为我在 AndroidManifest 中使用了属性 android:windowSoftInputMode="stateAlwaysVisible"。 但在这个活动中,我没有任何编辑文本(只有一个按钮),我需要阅读用户输入的每个字符(每次一次)。 所以我重写了 dispatchKeyEvent 来读取每个字符。
问题在于,由于正在显示键盘并且没有编辑文本,因此当单击任何字符时,Android 操作系统会选择屏幕上的按钮(或任何其他视图)。这个选择是我使用 D-pad 的一种选择。 如果按下后退按钮,它将从后退活动中“选择”任何其他视图。
我认为由于没有可与键盘一起使用的编辑文本,因此活动不知道如何处理键入的字符。
我在 Tiny 服务器上附加了一个简单的项目,其中包含两个活动,可用作重现问题的示例:http://s000.tinyupload.com/?file_id=70317553185010262971
还附上了 TinyPic 的截图:
下面也是我所有的代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testbug"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testbug.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.testbug.TestActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
</application>
</manifest>
MainActivity.java(1º 活动)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), TestActivity.class);
startActivity(i);
}
});
}
}
TestActivity.java(2º活动)
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
public class TestActivity extends Activity {
TextView tvType;
Button testButton;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count);
tvType = (TextView) findViewById(R.id.tv_type);
testButton = (Button) findViewById(R.id.btnTest);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
count++;
if(count==6){
hideKeyboard();
}
return super.dispatchKeyEvent(event);
}
private void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
布局:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
</LinearLayout>
activity_count.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TestButton"/>
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Press any key 3 times to Hide keyboard\n Then Press Back button. The same behavior of selection of button will happens on back activity" />
</LinearLayout>
【问题讨论】:
标签: android android-layout android-activity android-softkeyboard