2011-12-14 13 views
0

私の主なアクティビティにPakerfeldts' viewflow(具体的には 'DiffViewFlowExample')を実装しています。膨らんだビューにあるImageButtonにクリックリスナーを追加する方法を説明していません。追加する方法リスナーをインフレしたXMLボタンにクリックしますか?

これで、ダッシュボードビューにある@ 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

xmlのボタンにプロパティonClickを追加すると、実行するメソッドの名前が示されます。

このメソッドには、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 
} 

それが役に立てば幸い。

+2

私が知らなかったことの単純さ。 THNX !!これはうまくいった。ここではその使用方法に関するリンクです:[簡単なクリックリスナー](http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html)@ブログ。 – CelticParser

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

+0

正確には機能しません。あなたのアクティビティでfindViewById()を呼び出すか、activity.findViewById(..)のようにsthを実行する必要があります。 –

0

Buttonの代わりにTextViewを使用して、TextViewにクリックリスナーを書き込みます。

関連する問題