如何通过OK-Button

时间:2017-04-04 08:07:40

标签: java android

我刚在Android Studio中创建了一个小菜单,但现在我有问题是通过单击“确定”按钮来关闭我的上一个弹出窗口。

如果有人知道如何关闭这个弹出窗口,非常感谢。

package com.example.berni.myapplication1_1;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

/**
 * Created by Bernd on 02.04.2017.
 */

public class File_Open extends Activity {

  //      public File_Open mWindow;
  //      protected Context mContext;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.file_open);

    //  Start Fenster File_Open
    Button cc = (Button) findViewById(R.id.button_OK);
    cc.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {

        //------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------

        //   Here is the order from the OK - Button:
        Button btn = (Button) findViewById(R.id.button_OK);
        btn.setOnClickListener(this);

        //  Here I could start a new window: (it works)
        //  startActivity(new Intent(File_Open.this,Next.class));

        //  Here I want to close this popup-window (it does not work!!!)
        dismiss();

        //-----------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------
      }
    });

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int) (width * .5), (int) (height * .3));
  }

  private void dismiss() {
  }

}

3 个答案:

答案 0 :(得分:0)

您可以使用AlertDialog

@Override
protected void onCreate(Bundle savedInstanceState) {
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Alert");
    alertDialog.setMessage("Alert message to be shown");

    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

    Button cc = (Button) findViewById(R.id.button_OK);
    cc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              alertDialog.show();
        }
    });

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int) (width * .5), (int) (height * .3));
}

private void dismiss() {
}

答案 1 :(得分:0)

我认为你正在混合一些东西。您在活动级别创建了一个不是来自Activity的dismiss方法。 使用对话框时,Dialog类已实现此方法。 在Activity上调用dismiss方法时,不执行任何操作,因为这不是在Activity级别实现的。 如果您需要结束活动,请使用finish()。

答案 2 :(得分:0)

您将在这里编写所有导入语句。

public class BluetoothWindow extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_window);

        // here setting up height and width of the popup window.
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width*0.9),(int)(height*0.8));
        
        // Attaching close button with function.
        ImageButton close_button = findViewById(R.id.close_button);
        
        close_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeWindow();
            }
        });
    }
    public void closeWindow(){
        this.finish();
    }
}