Android 如何将单击侦听器添加到膨胀的XML按钮?
我正在我的主要活动中实现(特别是“DiffViewFlowExample”),我不知道如何将单击侦听器添加到膨胀视图中的ImageButtons 那么,如何向位于dashboard_视图中的带有@id=regulatoryDocsBtn的图像按钮添加单击侦听器呢 下面是代码片段&我提前感谢您的帮助 主要活动类别:Android 如何将单击侦听器添加到膨胀的XML按钮?,android,onitemclicklistener,layout-inflater,Android,Onitemclicklistener,Layout Inflater,我正在我的主要活动中实现(特别是“DiffViewFlowExample”),我不知道如何将单击侦听器添加到膨胀视图中的ImageButtons 那么,如何向位于dashboard_视图中的带有@id=regulatoryDocsBtn的图像按钮添加单击侦听器呢 下面是代码片段&我提前感谢您的帮助 主要活动类别: public class Home extends Activity { @Override protected void onCreate(final Bundle sav
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>
.....
您只需使用
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
}
});
应该这样做。所有这些都在中的示例中:)您只需使用
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
}
});
应该这样做。这都在示例中:)您可以将属性
onClick
添加到xml中的按钮,指示要执行的方法的名称
该方法必须具有视图
参数
例如:
在XML布局中:
<Button android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="testClick" />
希望有帮助。您可以将属性
onClick
添加到xml中的按钮,指示要执行的方法的名称
该方法必须具有视图
参数
例如:
在XML布局中:
<Button android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="testClick" />
希望有帮助。不要使用
按钮使用TextView
并在TextView
上编写click listener,而不要使用按钮使用TextView
并在TextView上编写click listener,这是我最初尝试的想法,但不起作用-只是NPE。但有什么不起作用?因为它应该工作:)你应该在你的活动中调用findviewbyd(),或者做一些类似于activity.findviewbyd(..)的事情。这是我最初尝试的想法,但它不工作-只是NPE。但有什么不起作用?因为它应该工作:)你应该在你的活动中调用findviewbyd(),或者做类似于activity.findviewbyd(..)的事情。我不知道的事情的简单性。THNX!!这起作用了。这里有一个关于它如何使用的链接:@在博客的底部。我不知道的事情的简单性。THNX!!这起作用了。这里有一个关于它如何使用的链接:@在博客的底部。