Android中可点击的TextView

20
我正在开发一款Android应用程序,其中包含许多文本视图,并希望这些视图可点击。
我尝试将android:clickable="true"android:onClick="clickHandler"属性分配给单个TextView,当应用程序触发clickHandler(View v)时,可以通过v.getId()正确获取被点击的项目。
但是我不想为每个TextView定义android:clickableandroid:onClick属性... 是否有什么方法可以通过代码来实现: "所有文本视图都可点击,并且单击处理在clickHandler中进行"?
谢谢。
6个回答

22
您可以像下面这样做 - 这样它们都有相同的处理程序:
public class sticks extends Activity implements View.OnTouchListener { 
  private TextView tv1; 
  private TextView tv2;
  private TextView tv3;

  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    tv3 = (TextView) findViewById(R.id.tv3); 

    // bind listeners
    tv1.setOnTouchListener(this); 
    tv2.setOnTouchListener(this); 
    tv3.setOnTouchListener(this); 

  } 

  @Override 
  public boolean onTouch(View v, MotionEvent event) { 
    // check which textview it is and do what you need to do

    // return true if you don't want it handled by any other touch/click events after this
    return true; 
  } 
}

这是工作代码还是伪代码?我无法按原样运行它。但还是谢谢!点赞! :) - tony gil

15

我使用了这个:

        <TextView
            android:id="@+id/txtTermsConditions"
            android:layout_width="180dp"
            android:layout_height="50dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:onClick="onTermConditions"
            android:clickable="true"
            android:focusable="true"
            android:text="Terms and Conditions"
            android:textColor="@color/blue" />

1
在这个例子中,android:textColorLink没有任何作用,除非文本包含实际的超链接。 - Sofi Software LLC
1
根据代码检查器,还需要添加 android:focusable="true" - lionello

7

你需要使用以下两段代码:

  <TextView
        android:id="@+id/reg_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_btn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:clickable="true"
        android:text="Are you now need to create an account?"
        android:textColor="#ff0000ff"
        android:textSize="15sp"
        />

And

private TextView tv1;
        tv1= (TextView)findViewById(R.id.reg_text);
    tv1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            ProgressDialog progressBar = ProgressDialog.show(MainActivity.this, "Title",
                    "شکیبا باشید!");
            progressBar.setCancelable(true);
            Intent i = new Intent(getApplicationContext(), Register.class);
            startActivity(i);
            return false;
        }
    });

4

0

下面的代码对我有用:

添加到strings.xml文件中:

 <string name="about_us"><font fgcolor="#039BE5">\"THIS IS SPARTA! more info at" <font fgcolor="#000000"><a href="https://google.com/">google.com</a></font>
</string>

添加到TextView:

android:linksClickable="true"

添加到活动:

about_us.movementMethod = LinkMovementMethod.getInstance()

这仍然需要为每个TextView设置act,这是@Cris想要避免的...你应该详细说明一下movementMethod(一个有趣的解决方案)。 - yakobom

0

你好,你必须同时使用以下两段代码:

  1. 使用XML文件代码

    <com.google.android.material.textview.MaterialTextView
     android:id="@+id/tx_demo"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Demo World!" />
    
  2. 使用JAVA文件代码

    txDemo = findViewById(R.id.tx_demo);
     txDemo.setOnClickListener(v -> {
         Toast.makeText(this, "TextView Click", Toast.LENGTH_SHORT).show();
     });
    

或者 2)使用Kotlin文件代码

 txDemo = findViewById(R.id.tx_demo)
    txDemo!!.setOnClickListener {
        Toast.makeText(
            this,
            "TextView Click",
            Toast.LENGTH_SHORT
        ).show()
    }

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