Android 不推荐使用 showDialog。什么是替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10285047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:09:31  来源:igfitidea点击:

showDialog deprecated. What's the alternative?

androiddialog

提问by Denis

Is there something else that should be called?

还有什么应该叫的吗?

showDialog(TIME_DIALOG_ID);

It's in this tutorialbut says deprecatedin Eclipse.

它在本教程中,但在 Eclipse 中已弃用

采纳答案by Md Mahbubur Rahman

From http://developer.android.com/reference/android/app/Activity.html

来自http://developer.android.com/reference/android/app/Activity.html

public final void showDialog (int id) Added in API level 1

This method was deprecated in API level 13. Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.

Simple version of showDialog(int, Bundle) that does not take any arguments. Simply calls showDialog(int, Bundle) with null arguments.

public final void showDialog (int id) 在 API 级别 1 中添加

此方法在 API 级别 13 中已弃用。请改用带有 FragmentManager 的新 DialogFragment 类;这也可以通过 Android 兼容包在旧平台上使用。

不带任何参数的 showDialog(int, Bundle) 的简单版本。只需使用空参数调用 showDialog(int, Bundle) 即可。

Why

为什么

  • A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.
  • Here is a nice discussion Android DialogFragment vs Dialog
  • Another nice discussion DialogFragment advantages over AlertDialog
  • 一个显示对话框窗口的片段,浮动在其活动窗口的顶部。该片段包含一个 Dialog 对象,它根据片段的状态适当显示。对话框的控制(决定何时显示、隐藏、关闭)应该通过这里的 API 来完成,而不是直接调用对话框。
  • 这是一个很好的讨论 Android DialogFragment vs Dialog
  • 另一个很好的讨论 DialogFragment 优于 AlertDialog

How to solve?

怎么解决?

More

更多的

回答by Matt Ball

From Activity#showDialog(int):

来自Activity#showDialog(int)

This method is deprecated.
Use the new DialogFragmentclass with FragmentManagerinstead; this is also available on older platforms through the Android compatibility package.

此方法已弃用。
使用新的DialogFragmentFragmentManager代替;这也可以通过 Android 兼容包在旧平台上使用。

回答by Khay

To display dialog box, you can use the following code. This is to display a simple AlertDialog box with multiple check boxes:

要显示对话框,可以使用以下代码。这是为了显示一个带有多个复选框的简单 AlertDialog 框:

AlertDialog.Builder alertDialog= new AlertDialog.Builder(MainActivity.this); .
            alertDialog.setTitle("this is a dialog box ");
            alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(),"ok ive wrote this 'ok' here" ,Toast.LENGTH_SHORT).show();

                }
            });
            alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(), "cancel ' comment same as ok'", Toast.LENGTH_SHORT).show();


                }
            });
            alertDialog.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), items[which] +(isChecked?"clicked'again i've wrrten this click'":"unchecked"),Toast.LENGTH_SHORT).show();

                }
            });
            alertDialog.show();

Heading

标题

Whereas if you are using the showDialog function to display different dialog box or anything as per the arguments passed, you can create a self function and can call it under the onClickListener()function. Something like:

而如果您使用 showDialog 函数根据传递的参数显示不同的对话框或任何内容,则可以创建一个 self 函数并可以在该onClickListener()函数下调用它。就像是:

 public CharSequence[] items={"google","Apple","Kaye"};
public boolean[] checkedItems=new boolean[items.length];
Button bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt=(Button) findViewById(R.id.bt);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            display(0);             
        }       
    });
}

and add the code of dialog box given above in the function definition.

并在函数定义中添加上面给出的对话框代码。

回答by MishaLee

This code worked for me. Easy fix but probably not a preferred way.

这段代码对我有用。易于修复,但可能不是首选方式。

public void onClick (View v) {
    createdDialog(0).show(); // Instead of showDialog(0);
}

protected Dialog createdDialog(int id) {
    // Your code
}

回答by Keshav Gera

package com.keshav.datePicker_With_Hide_Future_Past_Date;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    EditText ed_date;
    int year;
    int month;
    int day;

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

        ed_date=(EditText) findViewById(R.id.et_date);

        ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        });
    }
}


// Its Working