Android的 - 在画廊EditTexts显示奇怪的行为时,是(长)-clicked [英] Android - EditTexts in Gallery show strange behaviour when being (long)-clicked

查看:173
本文介绍了Android的 - 在画廊EditTexts显示奇怪的行为时,是(长)-clicked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序是基于谷歌的Hello画廊例如:
http://developer.android.com/guide/tutorials/views/hello -gallery.html
而不是使用的图像,我在构造函数中创建一批的EditText 取值。

我的问题是现在:当我长点击一个EditText,我想显示其上下文菜单(与全选,复制等)。我试着设置了 OnItemLongClickListener ,它通过 myGallery.getAdapter()调用所选择的看法。getView(位置,...)。showContextMenu() ,但运行到的StackOverflowError (这是顺便说一句,为什么我贴我的问题在这里的原因 - 好吧,那一个是个瘸子。):

  08-13 16:02:36.062:ERROR / AndroidRuntime(3400):致命异常:主要
java.lang.StackOverflowError
 在android.widget.AdapterView.getPositionForView(AdapterView.java:581)
 在android.widget.Gallery.showContextMenuForChild(Gallery.java:1049)
 在android.view.View.showContextMenu(View.java:2520)
 在de.test.gallery2.Main $ 1.onItemLongClick(Main.java:51)
 在android.widget.Gallery.dispatchLong preSS(Gallery.java:1074)
 在android.widget.Gallery.showContextMenuForChild(Gallery.java:1055)
 

我也试过 registerForContextMenu()图库,那么 EditTexts ,然后两个,但一切都失败了。请问你anbody有一个解决办法?

顺便说一句,画廊展示了一些其他奇怪的行为:应用程序启动时,第一个的EditText 为中心,但不能编辑,当我点击就可以了。但是,当我敲击第二个(不居中),我可以编辑一个没有它为中心。当我中心的第二次的EditText ,我只能编辑第三个等。当我中心是最后一个,焦点似乎完全消失,没有什么可以再进行编辑。

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

m1ntf4n

修改

下面是我的活动的code。很抱歉的双重职务,没有考虑编辑考虑的可能性。

 公共类主要扩展活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        最后图库图库=(图库论坛)findViewById(R.id.gallery);
        gallery.setAdapter(新LocalAdapter(本));
        gallery.setSpacing(50);

        registerForContextMenu(画廊);

        //注册EditViews的文本菜单。
        的for(int i = 0; I< gallery.getAdapter()getCount将(); ++ I){
            registerForContextMenu(gallery.getAdapter()getView(我,NULL,NULL));
        }

        //这个监听器会造成的StackOverflowError。
        /*gallery.setOnItemLongClickListener(new Gallery.OnItemLongClickListener(){
            @覆盖
            公共布尔onItemLongClick(适配器视图<>一种,视图V,INT I,长L){
                返回gallery.getAdapter()getView(我,NULL,NULL).showContextMenu()。
            }
        }); * /
    }

    公共类LocalAdapter扩展了BaseAdapter {
        私人语境mContext;
        私人的EditText [] EDITTEXT;

        公共LocalAdapter(上下文C){
            mContext = C;
            EDITTEXT =新的EditText [5];
            的for(int i = 0; i = editText.length;!++我){
                EDITTEXT [我] =新的EditText(mContext);
                EDITTEXT [I] .setLayoutParams(新Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                EDITTEXT [I] .setText(TEXT+ I);
                EDITTEXT [I] .setTextSize(30);
            }
        }
        @覆盖
        公众诠释getCount将(){
            返回editText.length;
        }
        @覆盖
        公共对象的getItem(INT位置){
            返回的位置;
        }
        @覆盖
        众长getItemId(INT位置){
            返回的位置;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            返回EDITTEXT [位置]
        }
    }
}
 

解决方案

从谷歌的文档:

  

公共无效registerForContextMenu(查看视图)

     

注册一个上下文菜单中显示为给定的视图(多   视图可以显示上下文菜单)。这种方法将设置   View.OnCreateContextMenuListener的观点这项活动,所以   onCreateContextMenu(文本菜单,查看,ContextMenuInfo)将被称为   当它是时间显示上下文菜单。

正如你可以从文件看,onCreateContextMenu()将被称为主上下文菜单显示之前。你需要重写此方法来创建自定义的快捷菜单。

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];
        }
    }
}

解决方案

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.

这篇关于Android的 - 在画廊EditTexts显示奇怪的行为时,是(长)-clicked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆