没有布局的对话框活动

时间:2014-11-17 06:55:08

标签: android android-layout android-alertdialog

我正在尝试实现一个由服务(在异步线程中)触发的警报对话框。所以我决定通过一个活动(ShowAlert.class)实现警报对话框。当使用我的后台服务触发对话框时,它在后台显示我的默认布局。即使我没有在我的警报活动(ShowAlert.class)中设置任何布局。任何人都可以帮助我,如何从背景中删除默认布局。

注意:我对自定义布局不感兴趣。

enter image description here

CODE:

public class ShowAlert extends Activity
{
AlertDialog alertDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("message");
alertDialogBuilder.setTitle("Title");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("OK", 
new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
      Intent positveActivity = new Intent(getApplicationContext(),HomeMap.class);
      // Sets the Activity to start in a new, empty task
      positveActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      positveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(positveActivity);

   }
});
alertDialogBuilder.setNegativeButton("Cancel", 
new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
       alertDialog.cancel();
     }
});

alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

}

2 个答案:

答案 0 :(得分:3)

创建活动

public class AlertActivity extends Activity {

    TextView dialog_title;
    TextView dialog_message;

    @Override
    protected void onCreate (Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        this.requestWindowFeature (Window.FEATURE_NO_TITLE);
        getWindow ().setBackgroundDrawableResource (android.R.color.transparent);
        setContentView (R.layout.view_custom_alert);

        dialog_title = (TextView) findViewById (R.id.dialog_title);
        dialog_message = (TextView) findViewById (R.id.dialog_message);

        dialog_title.setText ("Alert!");
        dialog_message.setText ("Reminder text here....                                                   ");
    }
}

在你的清单中

<activity
        android:name="com.telmate.custom.AlertActivity"
        android:excludeFromRecents="true"
        android:launchMode="singleInstance"
        android:taskAffinity=""
        android:theme="@android:style/Theme.Dialog" />

答案 1 :(得分:0)

        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();