简体   繁体   中英

TextView onclickListener

In my application i have added textviews dynamically using array,and given id's for each with a count variable, which increment's count according to the addition of textviews.In onClickListener of each textview i want to perform some oparations,but when i'm trying to do this operation is getting performed on all textviews. Below is the code,i'm not getting what's wrong.please help me.

// here i have added textview dynamically
       mtxtview[colTextCount]=new TextView(this);
       mtxtview[colTextCount].setId(colTextCount);
       mtxtview[colTextCount].setLayoutParams(new LayoutParams(20,20));

And In onclickListener-

@Override
public void onClick(View v) {
    System.out.println("onclick...");

    for(jj=0;jj<_mTextViewId;jj++){
        String hh=mtxtview[jj].getText().toString();
        System.out.println("................................."+hh);     
        System.out.println("id is...."+_mTextHeight[jj].getId());
    //if i added 3 textview.its giving me all 3 textview's text(getText())  

    }

           }

use switch-case and View.getId() to check which TextView is Clicked before starting for loop . try it as :

@Override
public void onClick(View v) {
    System.out.println("onclick...");
    switch(v.getId())
     {
       case _mTextViewId:  
         for(jj=0;jj<_mTextViewId;jj++){
          String hh=mtxtview[jj].getText().toString();
          System.out.println("................................."+hh);     
          System.out.println("id is...."+_mTextHeight[jj].getId());

         }
       break ;

      // same for others....       

    }

 }

see TextView onClick() not working

You should do setOnClickListener for each TextView Object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM