繁体   English   中英

Android 中的可点击 TextView

[英]Clickable TextView in Android

我正在构建一个 Android 应用程序,其中包含许多我希望可点击的 TextView。 我已经尝试将属性android:clickable="true"android:onClick="clickHandler"分配给单个TextView并且当应用程序正确触发 clickHandler v.getId() clickHandler(View v)时通过单击项。 . What I don't like is to define, for every TextView , the properties android:clickable and android:onClick ... is there something that i can do by code to say: "all the textviews are clickable and click is handled in clickHandler ?”

谢谢。

你可以在下面做这样的事情 - 这样他们都有相同的处理程序:

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; 
  } 
}

我用过这个:

        <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" />

您好您必须使用以下两个代码:

  <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"
        />

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;
        }
    });

我使用OnClickListener,尝试它而不是OnTouchListener.Here是一个如何使用它们的描述

下面的代码对我有用:

添加到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()

您好,您必须使用以下两个代码:

  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()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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