这是我的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()不起作用?
答案 0 :(得分:115)
有一种简单的方法。把它放在XML格式的TextView中:
android:clickable="true"
答案 1 :(得分:39)
使用这些
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"
答案 2 :(得分:7)
在android:clickable="true"
<TextView>
答案 3 :(得分:0)
在onCreate方法中,您需要:
您的类还需要实现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
}
}
答案 4 :(得分:-1)
请进行以下更改:
<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;
}
}
我认为这会对你有所帮助