TextView中的onClick()不工作 [英] TextView onClick() not working

查看:234
本文介绍了TextView中的onClick()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的$ C $下的main.xml

here's my code for 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>

这是我的$ C $下tabs.xml

here's my code for 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里面的code:

and here's the code inside the 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));
        }
    });

  }

所以,如果我设置的,而不是的setContentView(R.layout.main)在Main.class或Main.java文件的setContentView(R.layout.tabs)中的setContentView(),onClick的()的作品,我应该怎么做或什么地方错了我的code阻碍的onClick()不工作?

so, if I set the setContentView() in the Main.class or Main.java as setContentView(R.layout.tabs) instead of setContentView(R.layout.main), the onClick() works, what should I do or what's wrong with my code that hinders onClick() not to work?

推荐答案

使用这些

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

在上创建,然后设置

all.setOnclickListener(本)中的OnCreate()方法too.Implement onClicklistener时,它会显示错误。它的工作就像一个魅力。

all.setOnclickListener(this) in oncreate() method too.Implement onClicklistener when it will show error. it will work like a charm.

编辑

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"

这篇关于TextView中的onClick()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆