2011-12-14 68 views
0

我在我的主要活动中实施Pakerfeldts' viewflow(特别是'DiffViewFlowExample'),我无法确定如何将点击侦听器添加到处于虚拟视图中的ImageButton。如何将点击监听添加到充气的XML按钮?

那么如何将点击监听器添加到位于dashboard_view中的图像按钮w/an @ 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; 
} 

A的视图之一的片段: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> 

回答

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

你只需要获得使用

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的示例中:)

+0

这就是我最初试过的想法,但它不工作 - 只是NPE。 thnx虽然。 – CelticParser 2011-12-14 23:20:47

+0

什么不工作?因为它应该工作:)你应该在你的activity中调用findViewById(),或者像activity.findViewById(..)那样做。 – 2011-12-14 23:35:33

0

而不是Button使用TextView并写在该TextView点击监听器。

相关问题