android实现将普通的fragment转换成DialogFragment用于显示自定义的Dialog完整代码
时间: 2024-03-10 07:50:17 浏览: 72
下面是将普通的Fragment转换成DialogFragment用于显示自定义的Dialog的完整代码示例:
1. 自定义Dialog布局
在res/layout目录下创建一个名为dialog_my.xml的布局文件,用于定义自定义的Dialog的布局:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="My Dialog"/>
<EditText
android:id="@+id/dialog_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:gravity="end">
<Button
android:id="@+id/dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button
android:id="@+id/dialog_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
```
2. 创建DialogFragment子类
创建一个继承自DialogFragment的子类MyDialogFragment:
```java
public class MyDialogFragment extends DialogFragment {
private EditText mInputEditText;
public interface OnInputCompleteListener {
void onInputComplete(String inputText);
}
public MyDialogFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.dialog_my, container, false);
mInputEditText = (EditText) view.findViewById(R.id.dialog_input);
Button cancelButton = (Button) view.findViewById(R.id.dialog_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
Button okButton = (Button) view.findViewById(R.id.dialog_ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OnInputCompleteListener listener = (OnInputCompleteListener) getActivity();
if (listener != null) {
listener.onInputComplete(mInputEditText.getText().toString());
}
dismiss();
}
});
return view;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(params);
}
}
```
在onCreateView方法中,通过LayoutInflater将自定义的dialog_my布局文件解析为View,并添加点击事件处理。在onCreateDialog方法中,设置Dialog的样式。
3. 在Activity或Fragment中显示DialogFragment
在Activity或Fragment中,通过FragmentManager来显示MyDialogFragment,并实现OnInputCompleteListener接口:
```java
public class MainActivity extends AppCompatActivity implements MyDialogFragment.OnInputCompleteListener {
private TextView mInputTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInputTextView = (TextView) findViewById(R.id.input_text);
Button showDialogButton = (Button) findViewById(R.id.show_dialog_button);
showDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "MyDialogFragment");
}
});
}
@Override
public void onInputComplete(String inputText) {
mInputTextView.setText(inputText);
}
}
```
在onInputComplete方法中,处理用户输入完成后的逻辑。
这样就可以将普通的Fragment转换成DialogFragment,并显示出自定义的Dialog了。
阅读全文