如何在android中单击textview

时间:2012-11-21 06:25:06

标签: android xml android-widget textview

点击文字视图无效

我的xml是

<TextView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center" 
android:textColor="@color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>

和我的活动代码是

public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}  

但它不起作用。我无法点击该链接。请指导我

5 个答案:

答案 0 :(得分:2)

好吧,我使用以下代码使TextView可单击。

首先,在您的activity.xml中添加它,以使TextView成为可嵌入的:

<TextView
    android:id=android:id="@+id/button2" <!-- id to get this TextView -->
    (...)
    android:onClick="onClick"  <!-- Add the function onClick() -->
    android:clickable="true"   <!-- Set the boolean clickable to true -->
/>

然后,在MainActivity.java中添加:

private TextView textview;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);  

    // Get the TextView by id
    textview = (TextView) findViewById(R.id.button2);

    textview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              // TODO when clicked on your link(TextView)
        }
    });
}


正如我现在看到您正在尝试从TextView可点击链接创建一个链接,而不仅仅是能够单击TextView我将在下面的链接中找到一个类似的问题,在Stack Overflow中解决了您可能会发现有用的问题。< / p>


android开发人员对TextView的引用:
https://developer.android.com/reference/android/widget/TextView.html

Stack Overflow中关于如何在可点击的链接中建立链接的类似问题:
How do I make links in a TextView clickable?

答案 1 :(得分:1)

您可以像这样使用

<TextView
                            android:id="@+id/topPaid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:autoLink="all"
                            android:clickable="true"                           
                            android:text="@string/topPaid"
                            android:textColor="#000000"
                            android:textColorLink="#33CC33" />

和活动

TextView topPaid = (TextView) findViewById(R.id.topPaid);

Linkify.addLinks(topPaid, Linkify.ALL);

topPaid.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

//write ur logic
}
}

答案 2 :(得分:0)

TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b>  Text with a " + "<a href=\"http://www.google.com\">link</a> " + "Created in the Java source code using HTML."));

答案 3 :(得分:0)

XML代码

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

JAVA代码

    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setText(Html.fromHtml("<a href=\"http://www.google.com\">google</a> "));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 4 :(得分:0)

在您的XML中如下:

 <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:onClick="onTextViewPressed"
   />

在您附加的活动中 public void onTextViewPressed(View view){ ... }