2011-12-14 121 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點擊監聽器。

相關問題