【问题标题】:Back button in dialog对话框中的后退按钮
【发布时间】:2012-02-04 19:52:16
【问题描述】:

我试图让我的对话框中的后退按钮返回到原始屏幕。我不知道我是否拥有我需要的所有进口产品。谁能告诉我哪里出错了?

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>

【问题讨论】:

  • 抱歉,onclick 的延迟没有按我的意愿工作,但现在可以了。

标签: android dialog


【解决方案1】:
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) 设置内容视图

【讨论】:

  • 所以如果我采取 Intent intent = new Intent(DlogActivity.this, DlogActivity.class);然后把 dialog.cancel();它将告诉按钮从对话框返回页面
  • 它将关闭对话框并让您继续显示对话框的页面。如果您想返回上一页.. 使用 finish() 和 dialog.cancel
【解决方案2】:

通常情况下,后退按钮无需我们的帮助即可正常工作。如果你采取了

public void onBackPressed() {
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
}

出来,当你按下“返回”时会发生什么?如果这不是您想要的,那么您希望发生什么?如果没有错误,我认为您有所需的导入。
悬崖

【讨论】:

  • 我只想在用户按下按钮时关闭对话框页面上的一个按钮,一旦按下它,我就无法让它返回
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 2011-07-12
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多