对话框中的后退按钮
问题描述:
我试图让对话框中的后退按钮返回到原始屏幕。我不知道我是否有我需要的所有进口产品。有人能告诉我我要去哪里吗?对话框中的后退按钮
Java代码:
package my.dlog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DlogActivity extends Activity {
/** Called when the activity is first created. */
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main2);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
public void onClick(View v) {
dialog.show();
}
});
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:minHeight="400dp"
android:minWidth="300dp" android:background="@drawable/mint1">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:layout_width="236dp"
android:layout_height="220dp"
android:layout_marginRight="100dp" android:background="@drawable/carsee"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
答
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
dialog.cancel();
// Simply Dismiss the dialog to make it close and return to back..
/*What you are using is not a valid construct */
}
还要确保Button1的在主要布局因为你已经使用findViewById(R.id.button1)直接设置内容视图
答
那么通常后退按钮工作的理由没有我们的帮助。如果你把
public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}
了,当你按'返回'时会发生什么?如果这不是你想要的,那么你会发生什么?如果没有错误,我会认为你有必需的进口。
悬崖
+0
我只想在对话框页面上的一个按钮关闭时,ueser按下按钮我不能让它回去一旦iv按下它 – ravon30 2012-02-05 00:32:30
接受回答请 – Mun0n 2012-02-09 17:08:51
抱歉延迟onclick不工作,因为我想,但它的现在。 – ravon30 2012-02-28 18:14:43