2012-05-04 117 views

回答

1
@Override 
protected Dialog onCreateDialog(int id) { 
    // TODO Auto-generated method stub 
    switch (id) { 

    case 0: 

     final Dialog mDialog = new Dialog(this); 
     mDialog.setContentView(R.layout.customdialog); 
     mDialog.setTitle("Custom Dialog"); 
     mDialog.show(); 
     mDialog.setCancelable(false); 
     final EditText txtUser = (EditText)mDialog.findViewById(R.id.txtUserName); 


     Button btnButtonOK = (Button)mDialog.findViewById(R.id.btnOK); 
     Button btnButtonCancel = (Button)mDialog.findViewById(R.id.btnCancel); 

     btnButtonOK.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String user = txtUser.getText().toString(); 
       if (!user.equals("")) 
        { 
        Toast.makeText(getApplicationContext(), 
         user, Toast.LENGTH_LONG).show(); 
       mDialog.dismiss(); 
        } 
      } 
     }); 



     btnButtonCancel.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       mDialog.dismiss(); 
      } 
     }); 


     break; 
} 
+0

添加的組件是XML文件。 –

+0

@ user972890按照這個http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – Akram

1

,你可以讓你的奧尼德興有: progress_layer-XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:text="@string/layer_folien" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/layer_gesamnt" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <ProgressBar 
      android:id="@+id/progressBar2" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:id="@+id/dialog_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/layer_button" /> 

    </LinearLayout> 

和德代碼來調用它

final Dialog dialog = new Dialog(PechaKuchaTimerActivity.this); 
dialog.show(); 

final ProgressBar pr_fol = (ProgressBar) dialog.findViewById(R.id.progressBar1); 
final ProgressBar pr_ges = (ProgressBar) dialog.findViewById(R.id.progressBar2); 
final Button button_layerCancel = (Button) dialog.findViewById(R.id.dialog_cancel); 
1

我認爲你需要的代碼這裏是一個

mydialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

     <Button 
      android:id="@+id/ok" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Ok" 
      android:layout_gravity="center" 
      /> 

</LinearLayout> 

Dialog dialog=new Dialog(mContext); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.mydialog); 
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT; 
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground); 

Button mOkButton=(Button)dialog.findViewById(R.id.ok); 


mOkButton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 

    //do here what you want to do with ok button click 

    } 
}); 

這裏dialogbackground是背景圖像

+0

在這裏它沒有添加背景iaage ryte?需要一個背景圖片... –

+0

@ user972890 \t 在代碼中添加了dialog.getWindow()。setBackgroundDrawableResource(R.drawable.dialogbackground);在這種情況下,dialogbackground是圖像名稱 – Akram