不同的文本视图单击监听器 Android

3

我已经在Sherlock Fragment中为不同的TextView实现了单击侦听器,以下是我的代码:

  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...请帮忙。


1
你能否发布创建TextView的代码? - Raj
1
在你的类中使用这个implements OnClickListener。 - Dixit Patel
在XML中,我们可以为TextView的onclick属性指定相同的值吗? - user1526671
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;
    }

}

将您的活动实现为OnClickListener并未实现它,将添加onClick(View v)。在您的方法中处理您的点击事件。 - MuraliGanesan
朋友,我认为我的问题不在onclick上...请访问这个问题的链接:http://stackoverflow.com/questions/14641910/text-view-conflicting-android - user1526671

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();
}

朋友,我觉得我的问题不在于onclick...请访问这个问题:http://stackoverflow.com/questions/14641910/text-view-conflicting-android - user1526671

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接