2014-01-16 134 views
-2

我可以创建一个Android AlertDialog就像这样。设置Android AlertDialog的布局

AlertDialog.Builder MyDialog = new AlertDialog.Builder(this); 

但是是什么事项我怎么可以设置布局这一点。当我使用Dialog我可以初始化像这样设置布局

dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog); 
dialog.setContentView(R.layout.journey_details); 

我可以这样做来设置布局。或者在AlertDialog中没有这样的功能。

我第一次创建了一个自定义对话框,当我设置文本视图的时间时,它给出了一个空指针。这就是我尝试使用AlertDialog的原因。这是我的第一个代码。我无法弄清楚错误。

public void ShowJourneyDetails() { 
    dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog); 

    crnt_time=(TextView)findViewById(R.id.jd_txt_str_tim); 
    date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String crnt_date =date.format(new Date()).toString(); 
    crnt_time.setText(crnt_date); 

    } 

这给了我一个错误。那就是为什么我尝试了一个AlertDialog。

但我的AlterDialog也给了我一个NullPointer,错误来自按钮。我的代码是

public void ShowJourneyDetails() { 

    // dialog = new Dialog(Login.this,android.R.style.Theme_Holo_Dialog); 
    final AlertDialog.Builder MyDialog = new AlertDialog.Builder(this); 
    LayoutInflater factory = LayoutInflater.from(Login.this); 
    final View DialogView = factory.inflate(R.layout.journey_details, null); 
    MyDialog.setView(DialogView); 

    LoginIntent = new Intent(Login.this, DropPickUp_List.class); 
    check_fuel = (CheckBox) findViewById(R.id.jd_chk_ful); 
    check_e_oil = (CheckBox) findViewById(R.id.jd_chk_eoil); 
    check_wtr = (CheckBox) findViewById(R.id.jd_chk_wtr); 
    check_b_oil = (CheckBox) findViewById(R.id.jd_chk_boil); 
    check_lic = (CheckBox) findViewById(R.id.jd_chk_lc_inc); 
    check_gt_pass = (CheckBox) findViewById(R.id.jd_chk_gt_ps); 
    check_bills = (CheckBox) findViewById(R.id.jd_chk_bil); 
    crnt_time = (TextView) findViewById(R.id.jd_txt_str_tim); 
    dialogButton = (Button) dialog.findViewById(R.id.jd_btn_strt); 

    MyDialog.setView(check_fuel); 
    MyDialog.setView(check_e_oil); 
    MyDialog.setView(check_wtr); 
    MyDialog.setView(check_b_oil); 
    MyDialog.setView(check_lic); 
    MyDialog.setView(check_gt_pass); 
    MyDialog.setView(check_bills); 
    MyDialog.setView(dialogButton); 

    MyDialog.setView(crnt_time); 

    SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String crnt_date = date.format(new Date()).toString(); 
    crnt_time.setText(crnt_date); 

    final Stack<String> ItemStack=new Stack(); 

    dialogButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Toast.makeText(Login.this, msg, Toast.LENGTH_LONG).show(); 

     } 
    }); 

    MyDialog.show(); 

} 

我找不出错误。

+0

你想创建自定义AlertDialog,不是吗! – Tugrul

+0

创建自定义对话框 – jyomin

回答

2

如果你想创建自定义AlertDialog,那么你需要Inflate Layout象下面这样:

LayoutInflater factory = LayoutInflater.from(this); 
final View dialogView = factory.inflate(
      R.layout.custom_progress_layout, null); 

Layout设置为您Dialog象下面这样:

Dialog main_dialog = new Dialog(this, R.style.Theme_Dialog); 
    main_dialog.setContentView(dialogView); 

就是这样。

1

是的。你可以这样做:

LayoutInflater mLayoutInflator = LayoutInflater.from(this); 
final View customDialogView = mLayoutInflator.inflate(
     R.layout.journey_details, null); 
final AlertDialog customDialog= new AlertDialog.Builder(this).create(); 
customDialog.setView(customDialogView); 
customDialog.show();