我知道这是如此容易(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>

其他回答

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


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

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

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

这可能不是你想要的,但这是我正在做的事情。所有这些都在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) {
            ...
          }  
       }