Skip to content Skip to sidebar Skip to footer

How Do I Get A String From The Onclicklistener View? Android

I set up quite a few TextViews in a for loop, and each one gets .setClickable(true) Then, I tv.setOnClickListener(new OnClickListener() { //tv is the TextView. public void onCl

Solution 1:

Can you typecast as below?

publicvoidonClick(View v) {
            Stringtext=null;
            if(v instanceof TextView){
                TextViewt= (TextView) v;
                text = t.getText().toString();
            }
        }

Solution 2:

You can typecast the view to a textView and get the text. You can add a tag to the TextView and simply call v.getTag()

Solution 3:

@OverridepublicvoidonClick(View v) 
    { 
    Stringtext=null;
if(v instanceof TextView)
    {
     TextViewt= (TextView) v;
         text = t.getText().toString();
    }

    //this is for sending our clicked edittext value to next page ...

Intent next=newIntent(getApplicationContext(),sendSMS.class);
next.putExtra("msg",text);
startActivity(next);

    }

Post a Comment for "How Do I Get A String From The Onclicklistener View? Android"