在哪里放置活动代码?

时间:2013-08-03 11:36:20

标签: java android android-activity

所以我有一个检查某些内容的代码,并将其放在Activity的onCreate()中。我想知道将它放在那里是否正确,并且由于某种原因,检查主要活动的代码根本不起作用,第二个有吐司的代码工作。我认为问题可能出在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()方法,但它是这样的:当创建活动显示吐司时,按返回设置,按设置后退显示对话框,按钮不起作用。

4 个答案:

答案 0 :(得分:0)

在OnCreateDialog中编写AlertDialog代码并在OnCreate AsynTask中启动a以进行检查,一旦任务完成,在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);
}

答案 1 :(得分:0)

只要你启动应用程序并且它没有缓存在设备的RAM中,就会调用onCreate()。

我不明白你想要实现的目标除外,请通过添加更多代码来编辑你的帖子,我也会编辑我的答案以便更详细。

答案 2 :(得分: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();

            }
        });
}
}

答案 3 :(得分:0)

好的,我自己解决了,原来这只是逻辑缺失:D。遗憾!