[英]how to click on textview in android
单击文本视图不起作用
我的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);
}
但这不起作用。我无法单击该链接。请指导我
好吧,我使用下面的代码使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中解决的一个类似问题,可能对您有所帮助。
android开发人员对TextView的引用:
https://developer.android.com/reference/android/widget/TextView.html
Stack Overflow中有关如何在可点击的链接中建立链接的类似问题可能会有所帮助:
如何使TextView中的链接可点击?
您可以这样使用
<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
}
}
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."));
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());
在您的XML中,如下所示:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
在您附加的活动中,公共无效onTextViewPressed(View view){...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.