我有gridview,显示键盘字符
如何判断改变点击项目图像的方法
例如,当用户点击" D" " D"消失并用灰色img替换?
如果有可能给我看一个例子
谢谢你
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
// return mThumbIds.length;
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textview;
if (convertView == null) { // if it's not recycled, initialize some attributes
//textview = new TextView(mContext);
//textview.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//textview.setPadding(0, 0, 0, 0);
//
//
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
// textview = (TextView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
//textview.setText(alphabet[position]);
return imageView;
// return textview;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.al0, R.drawable.al1,
R.drawable.al2, R.drawable.al3,
R.drawable.al4, R.drawable.al5,
R.drawable.al6, R.drawable.al7,
R.drawable.al8, R.drawable.al9,
R.drawable.al10, R.drawable.al11,
R.drawable.al12, R.drawable.al13,
R.drawable.al14, R.drawable.al15,
R.drawable.al16, R.drawable.al17,
R.drawable.al18, R.drawable.al19,
R.drawable.al20, R.drawable.al21,
R.drawable.al22, R.drawable.al23,
R.drawable.al24, R.drawable.al25,
R.drawable.al26, R.drawable.al27,
R.drawable.al28, R.drawable.al29,
R.drawable.al30, R.drawable.al31,
R.drawable.al32
};
}
主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_game_layout);
GridView gridview= (GridView) findViewById(R.id.gridView2);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
ImageView imageshow = (ImageView) findViewById(R.id.imageView1);
imageshow.setImageResource(mThumbIds[position]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Integer[] mThumbIds = {
R.drawable.al0, R.drawable.al1,
R.drawable.al2, R.drawable.al3,
R.drawable.al4, R.drawable.al5,
R.drawable.al6, R.drawable.al7,
R.drawable.al8, R.drawable.al9,
R.drawable.al10, R.drawable.al11,
R.drawable.al12, R.drawable.al13,
R.drawable.al14, R.drawable.al15,
R.drawable.al16, R.drawable.al17,
R.drawable.al18, R.drawable.al19,
R.drawable.al20, R.drawable.al21,
R.drawable.al22, R.drawable.al23,
R.drawable.al24, R.drawable.al25,
R.drawable.al26, R.drawable.al27,
R.drawable.al28, R.drawable.al29,
R.drawable.al30, R.drawable.al31,
R.drawable.al32
};
private Integer[] mThumbIds1 = {
R.drawable.al32,R.drawable.al32,
R.drawable.al32,R.drawable.al32,
R.drawable.al32,R.drawable.al32
};
}
答案 0 :(得分:1)
在您的代码中,您有:
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { ... }
快速和简单的方法是使用参数查看v ,它是您点击的对象的视图。在此视图中,您可以尝试更改imageDrawable或backgound:
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
v.setImageDrawable(myIcon);
可能你必须管理点击其他图片以恢复此图片更改,这只是一个创业想法,希望这有帮助!
答案 1 :(得分:0)
在 onItemClick()方法中,第二个参数查看v 实际上是单击的视图。因此,如果只是 ImageView ,则将该参数转换为 ImageView ,然后您可以设置新图像。简单地说:
ImageView newImage = (ImageView)v;
newImage.setImageResource(put_some_image_id_here);