how to make text view clickable in android?

前端 未结 11 1535
轮回少年
轮回少年 2020-12-01 18:04

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 activit

相关标签:
11条回答
  • 2020-12-01 18:38

    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"
                    />
    
    0 讨论(0)
  • 2020-12-01 18:39

    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
    
                }
            });
    
    0 讨论(0)
  • 2020-12-01 18:41

    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
    }
    
    0 讨论(0)
  • 2020-12-01 18:42

    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);
       }
    });
    
    0 讨论(0)
  • 2020-12-01 18:43

    Try this:

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题