简体   繁体   English

Android GridView将焦点设置为第一个EditText

[英]Android GridView Set Focus to First EditText

I have a GridView that is populated by a layout containing an ImageView and an EditText. 我有一个由包含ImageView和EditText的布局填充的GridView。

When the activity is opened, is there a simple way to set the focus to the first EditText and pop open the keyboard? 当活动打开时,是否有一种简单的方法将焦点设置到第一个EditText并弹出以打开键盘?

After you create the view and set the content do the following 创建视图并设置内容之后,请执行以下操作

setContentView(R.Layout.main);
EditText edit = (EditText) findViewById(R.id.editText01);
edit.requestFocus();

That should do it. 那应该做。

EDIT: After rereading your post I realize you might be trying to access the item on the grid view in which case the above will not work. 编辑:重新阅读您的文章后,我意识到您可能正在尝试访问网格视图上的项目,在这种情况下,以上操作将无效。 Try this instead 试试这个

GridView myGridView = (GridView) findViewById(R.id.gridview);
ViewGroup griditem = (ViewGroup) myGridView.getChildAt(0); //First item
for(int i = 0; i < griditem.getChildCount(); ++i) {
    if(griditem.getChildAt(i) instaceof TextView)
        griditem.getChildAt(i).requestFocus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM