我正在我的主要活动中实现Pakerfeldts' viewflow(特别是'DiffViewFlowExample'),我无法想象如何将单击侦听器添加到我的虚增视图中的ImageButtons。
那么如何将一个点击监听器添加到位于dashboard_view中的@ id = regulatoryDocsBtn的图像按钮?
以下是代码段和&我提前感谢你的帮助。
主要活动类:
public class Home extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Setup the ViewFlow
* w/TitleFlow------------------------------------------
*/
ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow);
DiffAdapter vfAdapter = new DiffAdapter(this);
viewFlow.setAdapter(vfAdapter);
TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
indicator.setTitleProvider(vfAdapter);
viewFlow.setFlowIndicator(indicator);
}
DiffAdapter类:注意:膨胀的视图是@此类的底部。
public class DiffAdapter extends BaseAdapter implements TitleProvider {
private static final int VIEW1 = 0;
private static final int VIEW2 = 1;
private static final int VIEW_MAX_COUNT = VIEW2 + 1;
private final String[] names = { "DashBoard", "ToolBox" };
private LayoutInflater mInflater;
public DiffAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override(S)...............
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int view = getItemViewType(position);
if (convertView == null) {
switch (view) {
case VIEW1:
convertView = mInflater.inflate(R.layout.dashboard_view, null);
break;
case VIEW2:
convertView = mInflater.inflate(R.layout.toolbox_view, null);
break;
}
}
return convertView;
}
其中一个视图的片段:dashboard_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#10000000"
android:stretchColumns="*" >
<!-- Row 1 -->
<TableRow android:layout_weight="1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageButton
android:id="@+id/regulatoryDocsBtn"
style="@style/dashBoardimageBtn"
android:src="@drawable/ic_launcher_regulatory" />
<TextView
style="@style/dashBoardTxt"
android:layout_below="@id/regulatoryDocsBtn"
android:text="Regulatory Docs" />
</RelativeLayout>
.....
</TableRow>
答案 0 :(得分:4)
您可以将属性onClick
添加到xml中的按钮,指示要执行的方法的名称。
该方法必须有View
参数。
例如:
在XML布局中:
<Button android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="testClick" />
在活动代码中:
public void testClick(View v) {
// The code to be executed on click
}
希望它有所帮助。
答案 1 :(得分:1)
你只需要使用
获取按钮ImageButton rdb = (ImageButton) findViewById(R.id.regulatoryDocsBtn);
并为其添加一个监听器:
rdb.setOnClickListener(new RdbOnClickListener());
...
class RdbOnClickListener implements OnClickListener {
public void onClick(View v) {
//do what you gotta do
}
}
或简单地说:
rdb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do what you gotta do
}
});
应该这样做。这就是documentation中的例子:)