问题
I am developing an application in Eclipse build ID 20090920-1017 using android SDK 2.2 and testing on a Google Nexus One. For the purposes of the tests below I am using the IME "Android keyboard" on a non-rooted phone.
I have an EditText widget which exhibits some very strange behavior. I can type text, and then press the "del" key to delete that text; but after I enter a 'space' character, the "del" key will no longer remove characters before that space character.
An example speaks a thousand words, so consider the following two incredibly simple applications...
Example 1: An EditText in a LinearLayout widget:
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);
}
}
Run the above application, enter text "edit example", then press the "del" key several times until the entire sentence is deleted. Everything Works fine.
Now consider example 2: An EditText in a Gallery widget:
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);
}
}
Run the above application, enter text "edit example", then press the "del" key several times. If you are getting the same problem as me then you will find that you can't deleted past the 'space' character. All is not well.
If anyone could shed some light on this issue I would be most appreciative.
Regards
回答1:
I also encountered this problem. Instead of writing my own Gallery view I override the behavior of the Gallery view that causes this. It basically catches the del (and return) key events. My solution:
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;
}
}
I hope it saves some headaches for the next person having this problem :)
回答2:
I ended up writing my own gallery view, im just trying to look for a better implementation than asynctask for the transition animation threads. At least my edittext controls work now. Ill post up some sample code once ive got it sorted out. If anyones interested the android developers topics are: (Dang, I can only post once link). So search for "Soft keyboard “del” key fails in EditText on Gallery" and the other link is: What is best method for an animation worker thread?
来源:https://stackoverflow.com/questions/2976384/soft-keyboard-del-key-fails-in-edittext-on-gallery-widget