0

所以我有一个检查某些东西的代码,我把它放在onCreate()一个活动中。我想知道把它放在那里是否正确,而且由于某种原因,检查 Main Activity 的代码根本不起作用,第二个有 toast 的代码起作用。我认为问题可能出在 AlertDialog 中。这是带吐司的那个:

AlertDialog.Builder Dial = new AlertDialog.Builder(Screen.this);
Dial.setTitle(R.string.Dial_Tit);
Dial.setMessage(R.string.Dial_Mes);
Dial.setPositiveButton("OK", PosBC());
Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
Dial.show();

注意:两个按钮都有方法,我只是没有发布它们。问题是警报甚至没有显示。而且由于某种原因,吐司确实有效,它就像自动单击按钮一样,即使该方法具有不起作用的意图。

根据要求提供更多代码:

    private DialogInterface.OnClickListener NegBC() {
    Intent moveToStart;
    moveToStart = new Intent(Screen.this, Launch.class);
    startActivity(moveToStart);
    return null;
}

private DialogInterface.OnClickListener PosBC() {
    startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    Toast.makeText(getApplicationContext(), R.string.settingsToast, Toast.LENGTH_LONG).show();
    return null;
}

更新:我添加了create()显示对话框的方法,但它是这样的:创建活动时显示 toast,按返回进入设置,从设置显示对话框按返回,按钮不起作用。

4

4 回答 4

0

每当您启动应用程序并且它没有缓存在设备的 RAM 中时,都会调用 onCreate()。

除此之外,我不明白您要实现的目标,请通过添加更多代码来编辑您的帖子,我还将编辑我的答案以使其更详细。

于 2013-08-03T11:42:27.087 回答
0

好的,我自己解决了,结果只是缺少逻辑:D。对不起!

于 2013-08-04T12:07:24.340 回答
0

在 OnCreateDialog 中编写您的 AlertDialog 代码并在 OnCreate AsynTask中启动一个用于检查目的,一旦您的任务完成,在 onPostExecute 内部关闭对话框。

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

    showDialog(0x01);

}

@Override
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder Dial;
    switch (id) {

        case 0x01:
            Dial = new AlertDialog.Builder(Screen.this);
            Dial.setTitle(R.string.Dial_Tit);
            Dial.setMessage(R.string.Dial_Mes);
            Dial.setPositiveButton("OK", PosBC());
            Dial.setNegativeButton(R.string.Dial_NegBC, NegBC());
            Dial.create();
        break;

    default:
        break;
    }

    return super.onCreateDialog(id);
}
于 2013-08-03T11:40:25.467 回答
0

使用此代码在单击按钮时在 android 中显示 alertDialog:

     package .....; // name of your package.

        import android.app.Activity;
        import android.app.AlertDialog;
        import android.content.DialogInterface;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.Toast;


public class AlertDialogActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button btnAlertTwoBtns = (Button) findViewById(R.id.button1);

        btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // Creating alert Dialog with two Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("    "); //type your title here insid the quotes.

                // Setting Dialog Message
                alertDialog.setMessage(" "); //type the message which is to be displayed

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.ic_launcher); // set the icon from drawable folder just put the icon file in drawable folder.

                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); // just a sample code to tell that what things you can do here
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        });
}
}
于 2013-08-03T12:25:07.973 回答