GridView onclick没有使用EditText响应

时间:2015-07-01 11:51:07

标签: android gridview

我想要Gridview一张图片和EditText。我希望在点击时全屏显示图片,然后选择要在长按一下删除的Gridview项目。但GridView事件OnItemLongClickListener()OnItemClickListener()没有回复{{1虽然它们在设置EditText Edittext时有效。我尝试了很多不同的解决方案,但没有一个能够正常工作。这是我的代码:

activity_main.xml中

android:focusable="false"

grid_single.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" >

    <GridView
     android:numColumns="2"
     android:gravity="center"
     android:columnWidth="100dp"
     android:stretchMode="columnWidth"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/grid"
     />
   </LinearLayout>

Main_activity.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical"
android:id="@+id/lin"
 >

<ImageView
    android:id="@+id/grid_image"
    android:layout_width="50dp"
    android:layout_height="50dp">
</ImageView>

 <EditText
     android:id="@+id/grid_text"
     android:layout_width="150dp"
     android:layout_height="150dp"
     android:layout_marginTop="15dp" >

</EditText>
</LinearLayout>

CustomGrid.java

//import not included
public class MainActivity extends Activity {
GridView grid;

String[] web = {
        "Google",
        "Github",
        "Instagram",
        "Facebook",


} ;
int[] imageId = {
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher

};
private LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
    grid=(GridView)findViewById(R.id.grid);
            grid.setAdapter(adapter);

            grid.setOnItemLongClickListener(new   AdapterView.OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub

                       grid.setItemChecked(position, true);
                  View tv=(View) grid.getChildAt(position);
                  tv.setBackgroundColor(Color.LTGRAY);
                    return false;
                }


            });
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub
                     Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
                }
            }); 
}
} 

任何帮助将不胜感激..提前感谢..

2 个答案:

答案 0 :(得分:0)

尝试在EditText

中添加这些行
android:focusable="false"
android:focusableInTouchMode="false"

答案 1 :(得分:0)

所以终于解决了这个问题。 我使用了gesturelistener并检测到了运动。 谢谢。