如何在应用程序启动时显示自定义对话框?

huangapple go评论77阅读模式
英文:

How to display custom dialog on application launch?

问题

>有时候我想在应用启动时显示一个对话框。为此,我创建了一个名为LoginDialog的自定义对话框,并且在一个名为ApplicationClass的java类中使用了一个Application类。然而,我在显示这个对话框时遇到了问题。首先,我无法调用getsupportfragmentmanager()或类似的方法。而且,代码提示说loginDialog没有show方法,而我认为这是AppCompatDialogFragment类的标准操作。对于如何解决这个问题的任何建议都将不胜感激!

>代码:

public class ApplicationClass extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE);
        SharedPreferences.Editor sEditor = sharedPreferences.edit();
        if (sharedPreferences.getInt("EmailVer", 0) == 5) {
            showDialog();
        }
        Log.i("Abertura", "onCreate fired");
    }
    private void showDialog() {
        LoginDialog loginDialog = new LoginDialog();
        loginDialog.show(get);
    }
}
英文:

>Sometimes I want to display a dialog when the app launches. For that purpose I created a custom dialog named LoginDialog and I am using an Application class java. However, I am having trouble showing this dialog. For one I cannot call getsupportfragmentmanager() or anything alike. Plus, the code replies that loginDialog has no show method which I thought was a standard operation of the AppCompatDialogFragment class. Any tips on how to solve this problem are appreciated!

>The code:

public class ApplicationClass extends Application {
    
@Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences sharedPreferences = getSharedPreferences("Settings", MODE_PRIVATE);
        SharedPreferences.Editor sEditor = sharedPreferences.edit();
        if (sharedPreferences.getInt("EmailVer", 0) == 5) {
            showDialog();
        }
        Log.i("Abertura", "onCreate fired");
    }
    private void showDialog() {
        LoginDialog loginDialog = new LoginDialog();
        loginDialog.show(get);
    }
}

</details>


# 答案1
**得分**: 1

也许尝试在Activity类中显示对话框?您可以加载SharedPreferences并检查是否希望在Application类中显示对话框,但在Activity中显示对话框。代码如下所示:

ApplicationClass:
```java
import android.app.Application;
import android.util.Log;

public class ApplicationClass extends Application {
    private boolean showDialog;

    public boolean getShowDialog() {
        return showDialog;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("MyTag", "onCreate Application");

        // 检查是否需要显示对话框。主要逻辑在这里
        showDialog = true;
    }
}
```

MainActivity:
```java
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (((ApplicationClass) getApplication()).getShowDialog()) { // 获取应用并在`showDialog`为true时显示对话框
            Log.i("MyTag", "显示对话框");
            new AlertDialog.Builder(this)
                    .setTitle("标题")
                    .setMessage("消息")
                    .show();
        } else {
            Log.i("MyTag", "不显示对话框");
        }
    }
}
```

请注意,这只是代码的翻译部分,不包括您提供的问题。

<details>
<summary>英文:</summary>

Maybe try to show a dialog in the Activity class? You can load SharedPreferences and check if You want to show a dialog in the Application class but show a dialog in Activity. It will look like this:

ApplicationClass:
```
import android.app.Application;
import android.util.Log;

public class ApplicationClass extends Application
{
    private boolean showDialog;

    public boolean getShowDialog()
    {
        return showDialog;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.i(&quot;MyTag&quot;, &quot;onCreate Application&quot;);

        // check if You want dialog. Main logic here
        showDialog = true;
    }
}
```

MainActivity:
```
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (((ApplicationClass) getApplication()).getShowDialog()) //get application and show dialog if `showDialog` is true
        {
            Log.i(&quot;MyTag&quot;, &quot;Show dialog&quot;);
            new AlertDialog.Builder(this)
                    .setTitle(&quot;Title&quot;)
                    .setMessage(&quot;Message&quot;)
                    .show();
        }
        else
        {
            Log.i(&quot;MyTag&quot;, &quot;Do not show dialog&quot;);
        }
    }
}
```

</details>



huangapple
  • 本文由 发表于 2020年9月16日 02:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63907633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定