This is a custom dialog question for Android 6.0.

Asked 2 years ago, Updated 2 years ago, 92 views

I made a custom dialog including EditText as below. The problem is that it works well on Android version 5.x.x, but 6.x.From x and above The "Cancel" and "OK" buttons do not work. In addition, the dialog does not disappear when you press Back. What is the problem?

  final AlertDialog.Builder inputAlert = new AlertDialog.Builder(UserInfoActivity.this);
  inputAlert.setTitle ("Smart Plug Interworking");
  inputAlert.setMessage ("Please enter the ID and password required for login");

  final EditText edit_id = new EditText(UserInfoActivity.this);
  edit_id.setSingleLine();
  edit_id.setHint ("ID");

  final EditText edit_pw = new EditText(UserInfoActivity.this);
  edit_pw.setSingleLine();
  edit_pw.setHint ("password");

  LinearLayout layout = new LinearLayout(UserInfoActivity.this);
  layout.setOrientation(LinearLayout.VERTICAL);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  params.setMargins(50, 0, 50, 0);

  layout.addView(edit_id, params);
  layout.addView(edit_pw, params);
  inputAlert.setView(layout);

  inputAlert.setPositiveButton("OK", newDialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
  });
  inputAlert.setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
  });
  alertDialog = inputAlert.create();
  alertDialog.show();

dialog

2022-09-21 22:09

2 Answers

There was nothing wrong with the code, so I tested it on Nexus 6P (6.0.1). It works fine. If you click the Cancel or OK button, the callback is called normally. I think it's better to check if there's a problem with the test environment. I have attached a test video that disappears when you click the cancel button, so please check it.


2022-09-21 22:09

This is AppGraddle setting, I'm using target sdkVersion in 22 version due to library compatibility issue, but could it be because of this reason?

compileSdkVersion 23 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.kbell.smartplug" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" }

compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.android.support:design:23.2.1'

There's one thing here that doesn't change Haha


2022-09-21 22:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.