Android - 画廊中的编辑文本在被(长)单击时显示奇怪的行为

发布于 2024-11-29 13:57:44 字数 3558 浏览 2 评论 0原文

我的程序基于 Google 的 Hello Gallery 示例:
http://developer.android.com/guide/tutorials/views/hello -gallery.html
我没有使用图像,而是在构造函数中创建了一堆 EditText

我现在的问题是:当我长按 EditText 时,我希望显示其上下文菜单(带有“全选”、“复制”等)。我尝试设置一个 OnItemLongClickListener ,它通过 myGallery.getAdapter().getView(position, ...).showContextMenu() 调用选定的视图,但这会遇到a StackOverflowError (顺便说一句,这就是我在这里发布问题的原因 - 好吧,那个问题很蹩脚。):

08-13 16:02:36.062: ERROR/AndroidRuntime(3400): FATAL EXCEPTION: main
java.lang.StackOverflowError
 at android.widget.AdapterView.getPositionForView(AdapterView.java:581)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1049)
 at android.view.View.showContextMenu(View.java:2520)
 at de.test.gallery2.Main$1.onItemLongClick(Main.java:51)
 at android.widget.Gallery.dispatchLongPress(Gallery.java:1074)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1055)

我还尝试过 registerForContextMenu() Gallery,然后是 EditTexts,然后是两者,但一切都失败了。你们有解决方案吗?

顺便说一句,图库显示了一些其他奇怪的行为:当应用程序启动时,第一个 EditText 居中,但当我点击它时无法编辑。但是,当我点击第二个(不居中)时,我可以编辑那个不居中的。当我将第二个 EditText 居中时,我只能编辑第三个,依此类推。当我将最后一个居中时,焦点似乎完全消失,并且无法再编辑任何内容。

如果你能帮助我,我可能会嫁给你。任何帮助表示赞赏。相信我 - 在问这个问题之前我做了很多研究。真的。
非常感谢

m1ntf4n

编辑

这是我的活动的代码。抱歉,重复发帖,没有考虑到编辑的可能性。

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new LocalAdapter(this));
        gallery.setSpacing(50);

        registerForContextMenu(gallery);

        //Register the EditViews for ContextMenu.
        for(int i = 0; i < gallery.getAdapter().getCount(); ++i) {
            registerForContextMenu(gallery.getAdapter().getView(i, null, null));
        }

        //This listener will cause a StackOverflowError.
        /*gallery.setOnItemLongClickListener(new Gallery.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> a, View v, int i, long l) {
                return gallery.getAdapter().getView(i, null, null).showContextMenu();
            }
        });*/
    }

    public class LocalAdapter extends BaseAdapter {
        private Context mContext;
        private EditText[] editText;

        public LocalAdapter(Context c) {
            mContext = c;
            editText = new EditText[5];
            for(int i = 0; i != editText.length; ++i) {
                editText[i] = new EditText(mContext);
                editText[i].setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                editText[i].setText("TEXT " + i);
                editText[i].setTextSize(30);
            }
        }
        @Override
        public int getCount() {
            return editText.length;
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return editText[position];
        }
    }
}

my program is based on Google's Hello Gallery example:
http://developer.android.com/guide/tutorials/views/hello-gallery.html
Instead of using images, I create a bunch of EditTexts in the constructor.

My question is now: When I long click on an EditText, I want its Context Menu (with "select all", "copy" and so on) to be shown. I've tried setting an OnItemLongClickListener which calls the selected view via myGallery.getAdapter().getView(position, ...).showContextMenu(), but that runs into a StackOverflowError (that's btw the reason why I posted my question here - ok, that one was lame.):

08-13 16:02:36.062: ERROR/AndroidRuntime(3400): FATAL EXCEPTION: main
java.lang.StackOverflowError
 at android.widget.AdapterView.getPositionForView(AdapterView.java:581)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1049)
 at android.view.View.showContextMenu(View.java:2520)
 at de.test.gallery2.Main$1.onItemLongClick(Main.java:51)
 at android.widget.Gallery.dispatchLongPress(Gallery.java:1074)
 at android.widget.Gallery.showContextMenuForChild(Gallery.java:1055)

I have also tried to registerForContextMenu() the Gallery, then the EditTexts and then both, but everything failed. Does anbody of you have a solution?

Btw, the Gallery shows some other strange behaviour: When the application starts, the first EditText is centered but can't be edited when i tap on it. But when I tap on the second one (which is not centered), I can edit that one without it being centered. When I center the second EditText, I can only edit the third one and so on. When I center the last one, focus appears to vanish entirely and nothing can be edited anymore.

I will probably marry you if you can help me. Any help is appreciated. And believe me - I did a lot of research before asking this question. Really.
Thanks a lot

m1ntf4n

EDIT

Here is the code of my Activity. Sorry for the double post, didn't take the possibility of editing into consideration.

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new LocalAdapter(this));
        gallery.setSpacing(50);

        registerForContextMenu(gallery);

        //Register the EditViews for ContextMenu.
        for(int i = 0; i < gallery.getAdapter().getCount(); ++i) {
            registerForContextMenu(gallery.getAdapter().getView(i, null, null));
        }

        //This listener will cause a StackOverflowError.
        /*gallery.setOnItemLongClickListener(new Gallery.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> a, View v, int i, long l) {
                return gallery.getAdapter().getView(i, null, null).showContextMenu();
            }
        });*/
    }

    public class LocalAdapter extends BaseAdapter {
        private Context mContext;
        private EditText[] editText;

        public LocalAdapter(Context c) {
            mContext = c;
            editText = new EditText[5];
            for(int i = 0; i != editText.length; ++i) {
                editText[i] = new EditText(mContext);
                editText[i].setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                editText[i].setText("TEXT " + i);
                editText[i].setTextSize(30);
            }
        }
        @Override
        public int getCount() {
            return editText.length;
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return editText[position];
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-12-06 13:57:44

来自谷歌的文档:

public void registerForContextMenu(查看视图)

注册要为给定视图显示的上下文菜单(多个
视图可以显示上下文菜单)。该方法将设置
View.OnCreateContextMenuListener 在此活动的视图上,因此
onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 将被调用
何时显示上下文菜单。

从文档中可以看到,在显示上下文菜单之前,将在 Main 中调用 onCreateContextMenu() 。您将需要重写此方法来创建自定义上下文菜单。

From Google's documentation:

public void registerForContextMenu (View view)

Registers a context menu to be shown for the given view (multiple
views can show the context menu). This method will set the
View.OnCreateContextMenuListener on the view to this activity, so
onCreateContextMenu(ContextMenu, View, ContextMenuInfo) will be called
when it is time to show the context menu.

As you can see from the documentation, onCreateContextMenu() will be called in Main before the context menu is shown. You will need to override this method to create your custom context menu.

╭⌒浅淡时光〆 2024-12-06 13:57:44

谢谢,光荣的黑客。您引导我找到了以下解决方案:

  1. 在活动的 onCreate() 方法中,我们需要一个 registerForContext(myGallery)

  2. 创建您自己的扩展类 MyEditText 编辑文本。在此类中(当然,在添加构造函数之后),您需要使受保护的方法 EditText::onCreateContextMenu() 可访问:

    <前><代码>@Override
    公共无效onCreateContextMenu(ContextMenu菜单){
    super.onCreateContextMenu(菜单);
    }

  3. 返回活动,执行

    <前><代码>@Override
    公共无效onCreateContextMenu(ContextMenu contextMenu,查看v,
    ContextMenu.ContextMenuInfo menuInfo)
    {
    AdapterView.AdapterContextMenuInfo 信息 = (AdapterView.AdapterContextMenuInfo) menuInfo;
    ((MyEditText) info.targetView).onCreateContextMenu(contextMenu);
    }

    第一行是获取请求上下文菜单的视图,第二行是调用 EditText 的(现在可见)onCreateContextMenu(),依此类推长按用户习惯的上下文菜单。

Thanks, glorifiedHacker. You led me to the following solution:

  1. In the activity's onCreate() method, we require a registerForContext(myGallery).

  2. Create your own class MyEditText that extends EditText. In this class (after adding the constructors, of course), you need to make the protected method EditText::onCreateContextMenu() accessible:

    @Override
    public void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);
    }
    
  3. Back in the activity, do

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View v, 
                                    ContextMenu.ContextMenuInfo menuInfo) 
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        ((MyEditText) info.targetView).onCreateContextMenu(contextMenu);
    }
    

    The first line is getting the view which is requesting a context menu, the second one is calling the (now visible) onCreateContextMenu() of the EditText, so on a long press the contextmenu the user is used to appears.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文