3

我已经为夏洛克片段中的不同文本视图实现了单击侦听器这是我的代码

  getView().findViewById(R.id.tv_plan).setOnClickListener(btn1Listener);
  getView().findViewById(R.id.tv_plan1).setOnClickListener(btn1Listener);

和我的听众代码

   private View.OnClickListener btn1Listener = new View.OnClickListener() {

      @Override
      public void onClick(View v) 
      {
            android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
            String title=null;
            String tag=null;
               switch(v.getId())
              {
                case R.id.tv_plan:
                      System.out.println("Plan ids"  +v.getId());
                    title= "My Plan"; tag = "viewplan_dialog";
                    Utilsdialog dialog_vwplan = new Utilsdialog(context, R.layout.child,title, tag);
                    System.out.println("Before showing dialog tag");
                    dialog_vwplan.show(fm, tag);
                    System.out.println("SUCCESS");
                     break;
                case R.id.tv_plan1:
                    System.out.println("Plan ids"  +v.getId());
                    title= "Change plan"; tag = "changeplan_dialog";
                    Utilsdialog dialog_chgplan = new Utilsdialog(context, R.layout.child,title, tag);
                    dialog_chgplan.show(fm, tag);
                     break;

                 }
      }

  };

我的代码总是调用最后一个指定的监听器,即tv_plan1监听器,但我需要为两个文本视图提供 onclick 监听器。我当前的代码为两个文本视图点击调用 tv_plan1 ......请帮助

4

2 回答 2

2

试试这个,使用Button而不是TextView.

首先实现您的活动OnClickListener

 public class Auctions extends Activity implements OnClickListener {

   private Button menu_Btn;
private Button deal_Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.xml);
            menu_Btn = (Button) findViewById(R.id.menu);
    deal_Btn = (Button) findViewById(R.id.deals);
}  
 public void onClick(View v) {
    switch (v.getId()) {

     case R.id.menu:{
                  System.out.println("Plan ids"  +v.getId());
                title= "My Plan"; tag = "viewplan_dialog";
                Utilsdialog dialog_vwplan = new Utilsdialog(context, R.layout.child,title, tag);
                System.out.println("Before showing dialog tag");
                dialog_vwplan.show(fm, tag);
                System.out.println("SUCCESS");
                 break;
     }
            case R.id.deals:{
                System.out.println("Plan ids"  +v.getId());
                title= "Change plan"; tag = "changeplan_dialog";
                Utilsdialog dialog_chgplan = new Utilsdialog(context, R.layout.child,title, tag);
                dialog_chgplan.show(fm, tag);
                 break;
       }


    default:
        break;
    }

}
于 2013-02-01T06:38:56.847 回答
0

你能用这个吗?

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="test"
    android:text="Datepicker"
    android:textColor="#FFFFFF" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="test"
    android:text="Test 2"
    android:textColor="#FFFFFF" />

文档上

然后在您的活动中:

public void test(View v) {
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}
于 2013-02-01T06:55:11.177 回答