在我点击ImageView的地方,onclick方法不是hapenning。我的意思是它没有重定向到我的main.xml
//包名: bunk
//我的cesem.XML:
//只是一个Textview和一个图像视图 - 缩进四个空格。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Your Semester"
android:textSize="25dp"
android:gravity="center"
android:id="@+id/tvSemCe"
android:layout_marginBottom="20dp"
/>
<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back"
/>
</LinearLayout>
//Class file: Cesem.java
package com.bunk;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class Cesem extends Activity implements OnClickListener{
ImageView back;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
setContentView(R.layout.main);
}
}
}
答案 0 :(得分:0)
尝试将元素设置为可点击:
back.setClickable(true);
或者你可能看不到点击回调内容的反馈(setContentView ..)
尝试在点击回调中记录某些内容,例如
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == back) {
Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
setContentView(R.layout.main);
}
}
答案 1 :(得分:0)
if (v.getId() == R.id.back)
而不是
if (v == back)
答案 2 :(得分:0)
将您的代码更改为:
public class Cesem extends Activity implements OnClickListener{
ImageView back;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cesem); // cesem.xml
back=(ImageView) findViewById(R.id.back);// back is ImageView
back.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.back) {
Toast.makeText(getApplicationContext(), "Toast 1",Toast.LENGTH_SHORT).show();
Activity.this.setContentView(R.layout.main);
}
else
{
Toast.makeText(getApplicationContext(), "Toast 2",Toast.LENGTH_SHORT).show();
}
}
}
}
并在清单中将您的活动注册为:
<activity
android:name=".Cesem" />