【问题标题】:TextView onClick() not workingTextView onClick()不起作用
【发布时间】:2012-01-31 12:00:03
【问题描述】:

这是我的 main.xml 代码

 <merge xmlns:android="http://schemas.android.com/apk/res/android">
 <RelativeLayout 
    android:id="@id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


<include layout="@layout/tabs" />

<ScrollView
    android:fillViewport="true" 
    android:scrollbars="@null"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <LinearLayout 
        android:paddingTop="10dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- first text_view -->
        <TextView 
            android:background="@color/grey"
            android:textColor="@color/white"
            android:text="@string/category" 
            android:id="@+id/category1" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent"
            android:layout_marginTop="65dp" 
            android:textSize="17dp"
            android:typeface="serif"/>

        <!-- first horizontal_scrollview -->
        <HorizontalScrollView 
            android:scrollbars="@null"
            android:id="@+id/horizontalScrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout android:id="@+id/linearLayout1"
                android:orientation="horizontal" 
                android:visibility="visible"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">

                <!-- image_view should be here -->

            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</ScrollView>
</RelativeLayout>
</merge>

这是我的 tabs.xml 代码

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:background="#333333">

<TextView
    android:textColor="@color/gradient_green"
    android:id="@+id/viewall"
        android:layout_width="85dp"
        android:layout_height="25dp"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="17dp"
        android:textStyle="bold" 
        android:text="@string/view_all"
        android:onClick="onClick"
        android:focusable="false"
        android:clickable="true" />

    <TextView
        android:textColor="@color/white"
        android:id="@+id/pic"
        android:layout_width="45dp"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/viewall"
        android:textSize="17dp"
        android:textStyle="bold" 
        android:text="@string/pic"
        android:onClick="onClick"
        android:focusable="false"
        android:clickable="true" />
</RelativeLayout>

这是 Main.java 中的代码:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView all = (TextView) this.findViewById(R.id.viewall);
    TextView pic = (TextView) this.findViewById(R.id.pic);

    all.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView all = (TextView) findViewById(R.id.viewall);
            TextView pic = (TextView) findViewById(R.id.pic);

            Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();

            all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
            pic.setTextColor(getResources().getColorStateList(R.color.white));
        }
    });

    pdf.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView all = (TextView) findViewById(R.id.viewall);
            TextView pic = (TextView) findViewById(R.id.pic);

            Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();

            all.setTextColor(getResources().getColorStateList(R.color.white));
            pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        }
    });

  }

所以,如果我将 Main.class 或 Main.java 中的 setContentView() 设置为 setContentView(R.layout.tabs) 而不是 setContentView(R.layout.main),onClick() 有效,我应该怎么做阻止 onClick() 不起作用的代码有什么问题或有什么问题?

【问题讨论】:

  • 我认为这两个语句应该在 oncreate 或 onstart 方法中的其他地方 all = (TextView) this.findViewById(R.id.viewall); pdf = (TextView) this.findViewById(R.id.pic);像所有 = (TextView)findViewById(R.id.viewall); pdf = (TextView)findViewById(R.id.pic);
  • 我正在尝试显示像 Pulse News 应用这样的标签,并提出了这个想法,但我无法让它发挥作用
  • 我现在可以使用了,感谢您的回答,我坚持使用我的第一个邮政编码...

标签: android android-layout onclick textview


【解决方案1】:

有一个简单的方法。把它放在 XML 中的 TextView 中:

android:clickable="true"

【讨论】:

  • 哇...没有意识到 TextViews 在默认情况下是不可点击的。很高兴我遇到了你的答案。以为我在那里发疯了一会儿。
  • @Fernando JS,您能否支持详细说明为什么需要设置 android:clickable="true"?
  • 没有。使用 SDK 21 对我不起作用。Sameer 的回答很好。
  • 老板!在 Android 4.2.1 上运行良好。 TextView 默认不能点击,这是有道理的。但我认为当您设置onClick 值时,更高版本会自动将clickable 设置为true
【解决方案2】:

使用这些

 all = (TextView) this.findViewById(R.id.viewall);
 pdf = (TextView) this.findViewById(R.id.pic);

在创建然后设置

all.setOnclickListener(this) 也在 oncreate() 方法中。在 onClicklistener 显示错误时执行。它会像魅力一样发挥作用。

已编辑

TextView btn=(TextView) findViewById(R.id.accInfo);
btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //DO you work here
        }
    });

将 Clicklistenner 设置为 TextView 将自动使其可点击,因此无需

android:clickable="true"

【讨论】:

  • 除了您上面提到的内容之外,我还必须调用 btn.setFocusableInTouchMode(false); 来解决 TextView 需要双击才能实际触发 OnClick 事件的事实。我不知道过去两年可能发生了什么变化,但这似乎在 4.4.2 中是必需的。
  • 我得到:The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})
  • 看看你的导入,你应该导入android.view.View.OnClickListener;。我猜你为对话框监听器导入点击
  • 谢谢,只需在 xml 文件中添加“android clickable =true”就可以了!
【解决方案3】:

&lt;TextView&gt;中添加android:clickable="true"

【讨论】:

    【解决方案4】:

    在您的 onCreate 方法中,您需要:

    1. 为您的 TextViews 创建变量
    2. 为他们分配 onClickListener

    而且你的类也需要实现 OnClickListener。

    public class SqliteTestsActivity extends Activity implements OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TextView all = (TextView) R.findViewById(R.id.viewall);
            all.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            // Fill in this with your switch statement
        }
    }
    

    【讨论】:

    • 另外,有没有其他方法可以在没有实现 OnClickListener 的情况下实现它,因为我已经实现了另一个类
    • 首先你不能实现一个类,你只能实现接口。而且实现的接口没有数量限制。所以别着急,再实现一个接口,就是onclickListener
    【解决方案5】:

    请进行如下更改:

     <include  android:id = "@+id/lyttab" layout="@layout/tabs" />
    

    在您的 Java 类中

     public void onClick(View v){   
    
      View view = findViewById(R.id.lyttab);
    
     all = (TextView) view.findViewById(R.id.viewall);
     pif = (TextView) view.findViewById(R.id.pic);
    
    
    switch (v){
    
    case  all :
        Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
    
        all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        pic.setTextColor(getResources().getColorStateList(R.color.white));
    
        break;
    
    case pic:
        Toast.makeText(Main.this, "PDF", Toast.LENGTH_SHORT).show();
    
        all.setTextColor(getResources().getColorStateList(R.color.white));
        pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        break;
    
     }
     }
    

    我认为这会对你有所帮助

    【讨论】:

    • 您只能在 switch case 中设置整数值,而不是直接在 TextView @ramesh.Use all.getID() inplace of All
    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多