开发者

Android - EditTexts in Gallery show strange behaviour when being (long)-clicked

开发者 https://www.devze.com 2023-03-28 03:21 出处:网络
my program is based on Google\'s Hello Gallery example: http://developer.android.com/guide/tutorials/views/hello-gallery.html

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.vie开发者_StackOverflow中文版w.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消