自定义对话框中的按钮不会响应

时间:2016-06-11 19:14:33

标签: android

我正在尝试为我的应用程序的设置创建一个自定义对话框,问题是,自定义按钮,EditText&微调器不响应用户活动。我希望有人能帮帮忙。 这是我的代码:

ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentTitle("Title")
            .setContentUrl(Uri.parse("https://developers.facebook.com"))
            .setContentDescription(
                    "\"Description"\"")

}

和我对话框的xml:

public class SettingsDialog extends DialogFragment  {
private EditText editText;
private Spinner ipSpinner;
private MainActivity mainActivity;
private Settings settings;
private int selectedIP;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_settings, null);
    mainActivity = (MainActivity) getActivity();
    settings = mainActivity.getSettings();
    selectedIP = settings.getSelectedIP();
    editText = (EditText) dialogView.findViewById(R.id.edit_text_ip);
    ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip);
    ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String IP = editText.getText().toString();
            settings.setIP(selectedIP, IP);
            settings.settingsSave();
            mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
        }
    });
    ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            selectedIP = position;
            editText.setText(settings.getIP(selectedIP));
            mainActivity.showToast(Integer.toString(position));
            settings.settingsSave();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            return;
        }

    });
    builder.setView(inflater.inflate(R.layout.dialog_settings, null))

            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String IP = editText.getText().toString();
                    settings.setIP(selectedIP, IP);
                    settings.settingsSave();
                    mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SettingsDialog.this.getDialog().cancel();
                }
            });
    return builder.create();
}

感谢您的建议

1 个答案:

答案 0 :(得分:2)

您正在膨胀dialogView,然后为dialogView中的视图设置侦听器,但是当您为对话框构建器设置视图时,您会为新视图充气。

这是错误的。

 builder.setView(inflater.inflate(R.layout.dialog_settings, null))

这是正确的

 builder.setView(dialogView)