我正在使用android SDK 2.2在Eclipse构建ID 20090920-1017中开发应用程序并在Google Nexus One上进行测试.出于以下测试的目的,我在非根电话上使用IME“Android键盘”.
我有一个EditText小部件,它表现出一些非常奇怪的行为.我可以输入文字,然后按“del”键删除该文字;但在输入’space’字符后,“del”键将不再删除该空格字符前的字符.
一个例子说千言万语,所以请考虑以下两个非常简单的应用……
示例1:LinearLayout小部件中的EditText:
package com.example.linear.edit;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.LinearLayout;
public class LinearEdit extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT));
EditText edit = new EditText(getApplicationContext());
layout.addView(edit, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
setContentView(layout);
}
}
运行上面的应用程序,输入文本“编辑示例”,然后按“del”键几次,直到整个句子被删除.一切正常.
现在考虑示例2:Gallery小部件中的EditText:
package com.example.gallery.edit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.LinearLayout;
public class GalleryEdit extends Activity
{
private final String[] galleryData = {"string1", "string2", "string3"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Gallery gallery = new Gallery(getApplicationContext());
gallery.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, galleryData)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.MATCH_PARENT, Gallery.LayoutParams.MATCH_PARENT));
EditText edit = new EditText(getApplicationContext());
layout.addView(edit, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return layout;
}
});
setContentView(gallery);
}
}
运行上面的应用程序,输入文本“编辑示例”,然后按“del”键几次.如果您遇到与我相同的问题,那么您会发现无法删除“空格”字符.一切都不顺利.
如果有人能够对这个问题有所了解,我将非常感激.
问候
解决方法:
我也遇到过这个问题.我没有编写自己的Gallery视图,而是覆盖了导致此问题的Gallery视图的行为.它基本上捕获了del(和return)键事件.我的解决方案
public class PatchedGallery extends Gallery
{
public PatchedGallery(Context context)
{
super(context);
}
public PatchedGallery(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public PatchedGallery(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
boolean handled = false;
if (getFocusedChild() != null)
{
handled = getFocusedChild().dispatchKeyEvent(event);
}
if (!handled)
{
handled = event.dispatch(this, null, null);
}
return handled;
}
}
我希望它可以为下一个有这个问题的人节省一些麻烦:)