我的Android应用有问题。我有三个活动都通过图像按钮相互连接。
我的问题是我不能回到任何一个页面,只是转发。我一直在阅读关于后退按钮以及如何保持其状态而不是杀死(结束)它,因此用户也可以在需要时点击返回。
我是Android的新手,我知道有很多东西需要学习。谢谢你的帮助。
第一个Activity的Java代码:
package my.hope;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.content.Intent;
public class NewhopeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView myImage = (ImageView) findViewById(R.id.imagebutton1);
myImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(NewhopeActivity.this, Act2.class);
startActivity(intent);
}
});
}
}
XML代码:
<?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:orientation="vertical" android:background="@drawable/mint">
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="250dp"
android:background="@drawable/ic_launcher"
android:onClick="Act2"
android:src="@drawable/ic_launcher" />
</LinearLayout>
答案 0 :(得分:2)
单击设备上的后退按钮会发生什么?除非指定,否则它将终止当前活动并将您带回到上一个活动。 但是,如果您希望确切地指定单击后退按钮的功能,可以覆盖onBackPressed()方法:
@Override
public void onBackPressed() {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}