2011-01-06 36 views
11

Tôi đang tạo ứng dụng Android có nhiều TextView mà tôi muốn có thể nhấp được. Tôi đã cố gắng chỉ định các thuộc tính android:clickable="true"android:onClick="clickHandler" vào TextView và khi ứng dụng kích hoạt clickHandler(View v) Tôi nhận được mục được nhấp chính xác qua v.getId(). Điều tôi không thích là để xác định, cho mỗi TextView, các thuộc tính android:clickable và ... là có điều gì đó mà tôi có thể thực hiện bằng mã để nói: "tất cả các bản xem trước có thể nhấp và nhấp được xử lý trong clickHandler?"TextView có thể nhấp trong Android

Cảm ơn.

Trả lời

16

Bạn có thể làm một cái gì đó như thế này dưới đây - cách này họ đều có xử lý giống nhau:

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

là mã làm việc này hoặc giả? không thể làm cho nó hoạt động như vậy. nhưng tks anyway! CẬP NHẬT! :) –

11

Tôi sử dụng này:

 <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:text="Terms and Conditions" 
      android:textColor="@color/blue" /> 
+1

Trong ví dụ này, android: textColorLink không làm gì, trừ khi văn bản chứa siêu liên kết thực tế. –

+0

Tôi đoán bạn nói đúng. Tôi đã chỉnh sửa câu trả lời. –

3

Hi Bạn Phải sử dụng Cả hai bên dưới Code:

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

Dưới đây đang làm việc cho tôi:

add tới 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> 

thêm vào TextView:

android:linksClickable="true" 

thêm vào hoạt động:

about_us.movementMethod = LinkMovementMethod.getInstance() 
+0

Điều này vẫn có hành động thiết lập cho mỗi lần xem văn bản, mà @Cris muốn tránh ... Bạn nên xây dựng một chút về movementMethod (một giải pháp thú vị) – yakobom

Các vấn đề liên quan