【问题标题】:Soft keyboard "del" key fails in EditText on Gallery widget图库小部件上的 EditText 中的软键盘“del”键失败
【发布时间】:2011-08-09 23:29:25
【问题描述】:

我正在使用 android SDK 2.2 在 Eclipse build ID 20090920-1017 中开发一个应用程序,并在 Google Nexus One 上进行测试。出于以下测试的目的,我在非 root 手机上使用 IME“Android 键盘”。

我有一个 EditText 小部件,它表现出一些非常奇怪的行为。我可以输入文本,然后按“del”键删除该文本;但是在我输入“空格”字符后,“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); } }

运行上面的应用程序,输入文本“edit example”,然后按几次“del”键,直到整个句子被删除。一切正常。

现在考虑示例 2:图库小部件中的 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”键。如果您遇到与我相同的问题,那么您会发现无法删除“空格”字符。一切都不好。

如果有人能对这个问题有所了解,我将不胜感激。

问候

【问题讨论】:

    标签: android widget gallery android-edittext


    【解决方案1】:

    我也遇到过这个问题。我没有编写自己的 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;
        }
    }
    

    我希望它可以为遇到此问题的下一个人省去一些麻烦:)

    【讨论】:

      【解决方案2】:

      我最终编写了自己的画廊视图,我只是想为过渡动画线程寻找比 asynctask 更好的实现。至少我的 edittext 控件现在可以工作了。整理好后,我会发布一些示例代码。如果有人对 android 开发者的主题感兴趣:(Dang,我只能发布一次链接)。因此,搜索“软键盘“del”键在 Gallery 上的 EditText 中失败”,另一个链接是: What is best method for an animation worker thread?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多