开发者

how to make text view clickable in android?

开发者 https://www.devze.com 2023-03-10 09:01 出处:网络
is it possible in android tomake text view clickable if yes then how ??and if not then what willbe the way for make a label clickable??i want to implement a call activity using this

is it possible in android to make text view clickable if yes then how ??and if not then what will be the way for make a label clickable??i want to implement a call activity using this

private void call() {
     try {
         Intent callIntent = new Intent(Intent.ACTION_CALL);
         callIntent.setData(Uri.parse("tel:"+keywordxmlparsing.phone));
         startActivity(callIntent);
     } catch (ActivityNotFoundException activityException) {
         Log.e("dialing-example", "Ca开发者_StackOverflowll failed", activityException);
     }
 }

thanks for ur responses in advance...


textView.setOnClickListener(new View.OnClickListener());

Have you tried this?


More easier directly in the XML : with clickable = true

<TextView
                android:id="@+id/forgotPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:text="@string/forgotPassword"
                android:onClick="forgotPassword"
                android:clickable="true"
                />


We can also get click event on TextView same as Button & ImageView.

and method is also same for all View.

like as

view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });


First in your java file cast your TextView by xml id

TextView tv = (TextView)findViewById(R.Id.textView1);

then,

tv.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      // TODO Auto-generated method stub
   }
});


Honestly, I found a flat button to work better for what I was doing with a RecyclerView:

<Button
android:id="@+id/btnFoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"/>

Source: https://stackoverflow.com/a/30884132/2328637

Then customizing the button to fit my layout and finally adding the following to my MainActivity.java under onCreate:

Button btnFoo = (Button) findViewById(R.id.btnFoo);
btnFoo.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
      Intent intent = new Intent(MainActivity.this, FooActivity.class);
      startActivity(intent);
   }
});


Try this:

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }
});


In the xml for that TextView include the field

android:onClick="functionName"

Then in your java file associated with that xml include this function

public void functionName(View view){
// do your stuff
}


you can set a onclick listener to the texview like button.infact button inherits the properties from textview.


Though it was long ago when you asked your question. But I think that the right thing to attain what you wanted is to set TextView xml attribute android:autoLink. For example:

<TextView 
    ...
    android:autoLink="phone" />


Simply try this one:-

Implement View.OnClickListener, then simply apply switch case and define the id of your text view in the case and pass the intent.

example:-

     @Override
 public void onClick(View v) {
    //TODO Auto-generated method stub

   switch (v.getId()) {
case R.id.textView:
    startActivity(new Intent(this,CalledClass.class));

    break;

default:
    break;
}

//here textView is id for the textView I chose.


TextView is also derived of View like- EditText,ListView etc.,so we can use

textView.setOnClickListener(new View.OnClickListener());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号