gpt4 book ai didi

java - 带有自定义按钮的 ImageAdapter

转载 作者:行者123 更新时间:2023-11-30 03:48:31 25 4
gpt4 key购买 nike

我希望带有 ImageAdapter 的 GridView 具有自定义按下的按钮。我已经有了自定义按钮,但是 onClick 它会在按钮周围创建一个很大的橙色 onClick 东西。我只希望 onClick 显示我的自定义按下按钮。我该如何轻松做到这一点?

图像适配器:

public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] buttonValues;

public ImageAdapter(Context context, String[] buttonValues) {
this.context = context;
this.buttonValues = buttonValues;
}

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

if (convertView == null) {

gridView = new View(context);

// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);


// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);

String button = buttonValues[position];



if (button.equals("homework")) {
imageView.setImageResource(R.drawable.homework);
} else if (button.equals("schedule")) {
imageView.setImageResource(R.drawable.button_schedule);
} else if (button.equals("planner")) {
imageView.setImageResource(R.drawable.plannerbut);
} else {
imageView.setImageResource(R.drawable.settingsbut);
}

} else {
gridView = (View) convertView;
}

return gridView;
}

@Override
public int getCount() {
return buttonValues.length;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

}

还有我的 Activity(GridView 方法):

private void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);

gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "homework", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "schedule", Toast.LENGTH_SHORT).show();
break;

case 2:
Toast.makeText(getApplicationContext(), "planner",
Toast.LENGTH_SHORT).show();
break;

case 3:
Toast.makeText(getApplicationContext(), "settings",
Toast.LENGTH_SHORT).show();
break;
}

}
});

}

提前致谢!

沃特

最佳答案

您可以让您的按钮使用自定义可绘制对象,您可以在其中自行定义不同的状态。

<Button
android:id="@+id/mybutton"
android:background="@drawable/custom_button_selector" />

自定义按钮选择器.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/bg_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bgalt" />
<item android:drawable="@drawable/bg_normal" />
</selector>

然后在 bg_pressed/normal 定义不同的按钮。

看看http://www.mkyong.com/android/android-imagebutton-selector-example/

关于java - 带有自定义按钮的 ImageAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14546091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com