Android Fragment的使用 五 DalogFragment
来源:互联网 发布:小黄人接金币 源码 编辑:程序博客网 时间:2024/06/03 16:15
我这回是大家介绍一个封装了Fragment的DialogFragment类,因为AlertDialog遇到屏幕旋转的情况就会发生异常,但是这个类没有这个问题,所以Google也是推荐大家使用DialogFragment制作对话框,而且它能够使用AlertDialog使其不会受到屏幕旋转的影响。
使用例子如下:
public class MainActivity extends Activity implements LoginInputListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showEditDialog(View view) { EditNameDialogFragment editNameDialog = new EditNameDialogFragment(); editNameDialog.show(getFragmentManager(), "EditNameDialog"); } public void showLoginDialog(View view) { LoginDialogFragment dialog = new LoginDialogFragment(); dialog.show(getFragmentManager(), "loginDialog"); } @Override public void onLoginInputComplete(String username, String password) { Toast.makeText(this, "帐号:" + username + ", 密码 :" + password, Toast.LENGTH_SHORT).show(); }}
public class EditNameDialogFragment extends DialogFragment{ private Button ok; private Button cancel; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.fragment_edit_name, container, false); view.findViewById(R.id.id_cancel_edit_name).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onDestroyView(); } }); view.findViewById(R.id.id_sure_edit_name).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(),"ok",Toast.LENGTH_SHORT).show(); onDestroyView(); } }); return view; }}
public class LoginDialogFragment extends DialogFragment{ private EditText mUsername; private EditText mPassword; public interface LoginInputListener { void onLoginInputComplete(String username, String password); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.fragment_login_dialog, null); mUsername = (EditText) view.findViewById(R.id.id_txt_username); mPassword = (EditText) view.findViewById(R.id.id_txt_password); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(view) // Add action buttons .setPositiveButton("Sign in", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { LoginInputListener listener = (LoginInputListener) getActivity(); listener.onLoginInputComplete(mUsername .getText().toString(), mPassword .getText().toString()); } }).setNegativeButton("Cancel", null); return builder.create(); }}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showLoginDialog" android:text="DialogFragment" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showEditDialog" android:text="DialogFragment与AlertDialog" /></LinearLayout>
fragment_edit_name
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/id_label_your_name" android:layout_width="wrap_content" android:layout_height="32dp" android:gravity="center_vertical" android:text="Your name:" /><EditText android:id="@+id/id_txt_your_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/id_label_your_name" android:imeOptions="actionDone" android:inputType="text" /> <Button android:id="@+id/id_sure_edit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/id_txt_your_name" android:text="ok"/> <Button android:id="@+id/id_cancel_edit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/id_txt_your_name" android:text="cancel" android:layout_alignParentLeft="true"/></RelativeLayout>
fragment_login_dialog
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="64dp" android:background="#FFFFBB33" android:contentDescription="@string/app_name" android:scaleType="center" android:src="@drawable/title" /> <EditText android:id="@+id/id_txt_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginTop="16dp" android:hint="input username" android:inputType="textEmailAddress" /> <EditText android:id="@+id/id_txt_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_marginTop="4dp" android:fontFamily="sans-serif" android:hint="input password" android:inputType="textPassword" /></LinearLayout>
下一次将一些Fragment里面的高级用法。
再见
0 0
- Android Fragment的使用 五 DalogFragment
- Android Fragment的使用
- Android-Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- android Fragment 的使用
- Android Fragment的使用
- Android Fragment的使用
- Android---fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Dagger2 @Component 和@SubComponent 区别解惑
- HTTP权威指南读书笔记二:关于url的那些事
- Android Studio 使用过程中出现的问题汇总(一)
- sed
- C++菜鸟的成长之路(1)——浅谈原码、反码、补码
- Android Fragment的使用 五 DalogFragment
- Spring框架学习l
- C#中Dictionary<Key,Value>中[]操作的效率问题
- ZOJ1029
- 细说光刻技术
- (一)碎碎念接口优化--新代码提交问题
- Spring Boot实战
- MySQL存储过程实例
- 利用AOP为Spring Data Jpa的接口Repository添加全局自定义过滤