我知道这是如此容易(doh…),但我正在寻找一种方法来运行在一个Android应用程序点击或点击文本的TextView行。

我一直在想按钮监听器和匿名方法监听器调用,但它似乎并不适用于TextView。

有人能指出我在一些代码片段,以显示如何点击或点击在TextView运行一个方法的文本?


当前回答

在一个调用布局和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>

其他回答

你可以使用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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:onClick="onClick"
    android:clickable="true"

您还必须实现View。OnClickListener和OnClick方法可以使用意图

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
           intent.setData(Uri.parse("https://youraddress.com"));    
            startActivity(intent);

我测试这个解决方案工作良好。

虽然您可以通过将侦听器设置为textview来解决这个问题,但建议不要这样做。你应该使用平面按钮,因为它是按钮的子类,它提供了许多属性,TextView不。


要使用平面按钮,添加style="? "android:attr/borderlessButtonStyle属性-

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DONE"
    style="?android:attr/borderlessButtonStyle"/>

要点击一段文本(不是整个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 &lt;a href="http://www.stackoverflow.com"&gt;Link!&lt;/&gt;</string>

然后在你的textview中:

textView.setText(Html.fromHtml(getString(R.string.html)));

在一个调用布局和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>