2016-10-03 72 views
1

我有一个对话框片段,我想将ok/cancel按钮放在对话框的底部。我尝试过,但我唯一得到的是编辑文本上的按钮(即在DialogFragment中)。 Thisi是我的对话框: enter image description here将确定/取消按钮添加到DialogFragment

,这是我的对话框代码:

public class dialogNewFile extends DialogFragment { 
private EditText textNewFile; 

public dialogNewFile(){} 

public static dialogNewFile newIstance(String title){ 
    dialogNewFile frag=new dialogNewFile(); 
    Bundle args=new Bundle(); 
    args.putString("title",title); 
    frag.setArguments(args); 
    return frag; 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIstanceState){ 
    return inflater.inflate(R.layout.fragment_newfile,container); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedIstanceState){ 
    super.onViewCreated(view, savedIstanceState); 
    textNewFile=(EditText) view.findViewById(R.id.edit_text_newfile); 
    String title=getArguments().getString("title","Enter name"); 
    getDialog().setTitle(title); 
    textNewFile.requestFocus(); 
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
} 

@Override 
public void onResume() { 
    Window window = getDialog().getWindow(); 
    Point size = new Point(); 
    // Store dimensions of the screen in `size` 
    Display display = window.getWindowManager().getDefaultDisplay(); 
    display.getSize(size); 
    // Set the width of the dialog proportional to 75% of the screen width 
    window.setLayout((int) (size.x * 0.75), (int) (size.x * 0.50)); 
    window.setGravity(Gravity.CENTER); 
    // Call super onResume after sizing 
    super.onResume(); 

} 

这是对话片段的布局:

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

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="35dp" 
    android:hint="@string/hint_new_file" 
    android:inputType="text" 
    android:id="@+id/edit_text_newfile"/> 

</LinearLayout> 
+0

向我们展示xml布局文件。 – AAnkit

+0

为什么不使用AlertDialog并调用如下代码:new AlertDialog.Builder()。setPositiveButton(“Ok”,listener).setNegativeButton(“Cancel”,...)您还可以添加一个自定义视图.setView –

回答

3

您应覆盖onCreateDialog和使用AlertDialog.Builder设置正面和负面按钮如下所示:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    String title = getArguments().getString("title"); 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle(title); 
    builder.setMessage("Are you sure?"); 

    // Edited: Overriding onCreateView is not necessary in your case 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    View newFileView = inflater.inflate(R.layout.fragment_newfile, null); 
    builder.setView(newFileView); 

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // on success 
     } 
    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 

    return builder.create(); 
} 
+0

Thanks ,那样我就可以得到这些按钮,但是我的布局现在被覆盖了,如下所示:http://imgur.com/3KRfbuD –

+0

使用layoutInflater使用构建器来扩展布局。 – kolunar

+0

在我的例子中,我赢得了这个改变:LayoutInflater inflater = getActivity()。getLayoutInflater(); –

0

如果你想使用自定义对话框视图中,使用alertdialog.setView方法

 AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     View myview = alertDialog.getLayoutInflater().inflate(R.layout.custom_dialog_layout, null); 
     final AlertDialog alertDialog = builder.create(); 
     builder.setView(myview); 
     alertDialog .show(); 

对于点击事件。

Button positiveButton = myview.findViewById(R.id.btnPositive); 
positiveButton.setOnclickListener(new OnClickListener .....