Android 如何单击或点击 TextView 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3328757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to click or tap on a TextView text
提问by Droid
I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.
我知道这很简单(doh...)但我正在寻找一种方法来运行在 Android 应用程序中点击或单击 TextView 文本行的方法。
I keep thinking about button listeners and anonymous method listener calls, but it just does not seem to apply to TextView.
我一直在考虑按钮侦听器和匿名方法侦听器调用,但它似乎不适用于 TextView。
Can someone point me at some code snippet to show how clicking or tapping on a piece of text in a TextView runs a method?
有人可以指点我一些代码片段来展示如何单击或点击 TextView 中的一段文本来运行方法吗?
回答by Patrick Cullen
You can set the click handler in xml with these attribute:
您可以使用以下属性在 xml 中设置单击处理程序:
android:onClick="onClick"
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
不要忘记 clickable 属性,没有它,点击处理程序不会被调用。
main.xml
主文件
...
<TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
我的活动.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
回答by Joshua Sutherland
This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate
:
这可能不是您正在寻找的,但这对我正在做的事情有效。所有这一切都在我之后onCreate
:
boilingpointK = (TextView) findViewById(R.id.boilingpointK);
boilingpointK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ("Boiling Point K".equals(boilingpointK.getText().toString()))
boilingpointK.setText("2792");
else if ("2792".equals(boilingpointK.getText().toString()))
boilingpointK.setText("Boiling Point K");
}
});
回答by Droid
OK I have answered my own question (but is it the best way?)
好的,我已经回答了我自己的问题(但这是最好的方法吗?)
This is how to run a method when you click or tap on some text in a TextView:
这是当您单击或点击 TextView 中的某些文本时运行方法的方法:
package com.textviewy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class TextyView extends Activity implements OnClickListener {
TextView t ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener(this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
}
and my main.xml is:
我的 main.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<TextView android:text="This is my first text"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28dip"
android:editable = "true"
android:clickable="true"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
回答by tony gil
from inside an activity that calls a layout and a textview, this click listener works:
从调用布局和文本视图的活动内部,这个点击监听器工作:
setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View viewIn) {
try {
Log.d(TAG,"GMAIL account selected");
} catch (Exception except) {
Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
}
}
});
the text view is declared like this (default wizard):
文本视图声明如下(默认向导):
<TextView
android:id="@+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/menu_id_google"
android:textSize="30sp" />
and in the strings.xml file
并在strings.xml文件中
<string name="menu_id_google">Google ID (Gmail)</string>
回答by Confuse
Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat buttonas it is a subclass of Button and it provides many attributes which TextView doesn't.
虽然您可以通过将侦听器设置为 textview 来解决问题,但建议不要这样做。您应该使用平面按钮,因为它是 Button 的子类,并且它提供了许多 TextView 没有的属性。
To use flat button, add style="?android:attr/borderlessButtonStyle"
attribute -
要使用平面按钮,请添加style="?android:attr/borderlessButtonStyle"
属性 -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
回答by rp23b
in textView
在文本视图中
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
You must also implement View.OnClickListener and in On Click method can use intent
您还必须实现 View.OnClickListener 并且在 On Click 方法中可以使用 Intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
I tested this solution works fine.
我测试了这个解决方案工作正常。
回答by njzk2
To click on a piece of the text (not the whole TextView
), you can use Html
or Linkify
(both create links that open urls, though, not a callback in the app).
要单击一段文本(不是整个TextView
),您可以使用Html
或Linkify
(两者都创建打开网址的链接,但不是应用程序中的回调)。
Linkify
Linkify
Use a string resource like:
使用字符串资源,如:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Then in a textview:
然后在文本视图中:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Html
Using Html.fromHtml
:
使用Html.fromHtml
:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
Then in your textview:
然后在你的文本视图中:
textView.setText(Html.fromHtml(getString(R.string.html)));
回答by Marcelo Amaral
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
您可以将 TextWatcher 用于 TextView,它比 ClickLinstener 更灵活(不是最好也不是最差,只有一种方式)。
holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// code during!
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// code before!
}
@Override
public void afterTextChanged(Editable s) {
// code after!
}
});