Android 如何单击或轻触文本查看文本
我知道这很简单(doh…),但我正在寻找一种方法,在Android应用程序中点击或点击文本视图中的文本行来运行方法 我一直在考虑按钮侦听器和匿名方法侦听器调用,但它似乎并不适用于TextViewAndroid 如何单击或轻触文本查看文本,android,onclick,listener,textview,Android,Onclick,Listener,Textview,我知道这很简单(doh…),但我正在寻找一种方法,在Android应用程序中点击或点击文本视图中的文本行来运行方法 我一直在考虑按钮侦听器和匿名方法侦听器调用,但它似乎并不适用于TextView 有人能告诉我一些代码片段,说明在TextView中单击或点击一段文本是如何运行方法的吗?好的,我已经回答了我自己的问题(但这是最好的方法吗?) 这是在文本视图中单击或轻触某些文本时如何运行方法: package com.textviewy; import android.app.Activity; i
有人能告诉我一些代码片段,说明在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");
}
}
我的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>
这可能不是您想要的,但这正是我所做的工作的意义所在。所有这些都是在我的
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"/>
...
从调用布局和文本视图的活动内部,此单击侦听器工作:
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());
}
}
});
文本视图声明如下(默认向导):
在strings.xml文件中
<string name="menu_id_google">Google ID (Gmail)</string>
googleid(Gmail)
要单击一段文本(而不是整个TextView
),您可以使用Html
或Linkify
(两者都创建打开URL的链接,但不是应用程序中的回调)
Linkify
使用字符串资源,如:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Html
使用Html.fromHtml
:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
虽然可以通过将侦听器设置为textview来解决此问题,但建议不要这样做。您应该使用,因为它是Button的一个子类,并且它提供了TextView没有的许多属性
要使用平面按钮,请添加
style=“?android:attr/borderlessButtonStyle”
属性-
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
在文本视图中
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
我测试了这个解决方案,效果很好。您可以将TextWatcher用于TextView,它比ClickLinstener更灵活(不是最好的,也不是更糟,只有一种方法)
“不要忘记clickable属性,如果没有它,就不会调用click处理程序。”这一行在一天内保存了一次。非常感谢,伙计。需要提到一些关于clickable的内容。本来可以节省一个小时。有趣的是,使用
setOnClickListener
设置clickable
属性,但使用onClick
属性显然没有。似乎onClick
在Android 5.0棒棒糖(API 21)中设置了clickable
属性。也许他们认为这是一个错误,在旧版本中没有发生过?有没有办法通过XML长时间点击?你真的应该接受最上面的答案。他必须登录才能做到这一点。他还拿走了一个不错的用户名:/for future,如果有人正在使用Kotlin并希望通过回调完全控制可点击文本-我写了一篇关于它的文章,为TextView
提供扩展功能-您可能还希望添加android:textAllCaps=“false”
,以允许小写文本。
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
textView.setText(Html.fromHtml(getString(R.string.html)));
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
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!
}
});