我知道这是如此容易(doh…),但我正在寻找一种方法来运行在一个Android应用程序点击或点击文本的TextView行。
我一直在想按钮监听器和匿名方法监听器调用,但它似乎并不适用于TextView。
有人能指出我在一些代码片段,以显示如何点击或点击在TextView运行一个方法的文本?
我知道这是如此容易(doh…),但我正在寻找一种方法来运行在一个Android应用程序点击或点击文本的TextView行。
我一直在想按钮监听器和匿名方法监听器调用,但它似乎并不适用于TextView。
有人能指出我在一些代码片段,以显示如何点击或点击在TextView运行一个方法的文本?
当前回答
要点击一段文本(不是整个TextView),你可以使用Html或Linkify(两者都创建链接,打开url,虽然不是在应用程序中的回调)。
其内
使用字符串资源,如:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
然后在textview中:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
使用Html.fromHtml:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
然后在你的textview中:
textView.setText(Html.fromHtml(getString(R.string.html)));
其他回答
这可能不是你想要的,但这是我正在做的事情。所有这些都在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");
}
});
你可以用这些属性在xml中设置点击处理程序:
android:onClick="onClick"
android:clickable="true"
不要忘记clickable属性,没有它,就不会调用click处理程序。
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
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
你可以使用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!
}
});
在一个调用布局和textview的活动内部,这个点击监听器工作:
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());
}
}
});
文本视图是这样声明的(默认向导):
<TextView
android:id="@+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/menu_id_google"
android:textSize="30sp" />
在strings.xml文件中
<string name="menu_id_google">Google ID (Gmail)</string>
要点击一段文本(不是整个TextView),你可以使用Html或Linkify(两者都创建链接,打开url,虽然不是在应用程序中的回调)。
其内
使用字符串资源,如:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
然后在textview中:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
使用Html.fromHtml:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
然后在你的textview中:
textView.setText(Html.fromHtml(getString(R.string.html)));