设为首页 加入收藏

TOP

Android 官方推荐:DialogFragment 创建对话框(二)
2015-08-31 21:25:00 来源: 作者: 【 】 浏览:76
Tags:Android 官方 推荐 :DialogFragment 创建 对话
arent"
? ? ? ? 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" />


? ? ? ? ? ? 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" />



b)继承DialogFragment重写onCreateDialog方法


package com.example.zhy_dialogfragment;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


public class LoginDialogFragment extends DialogFragment
{


?@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);
? // 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)
? ? ? ?{
? ? ? ?}
? ? ? }).setNegativeButton("Cancel", null);
? return builder.create();
?}
}


c)调用


public void showLoginDialog(View view)
?{
? LoginDialogFragment dialog = new LoginDialogFragment();
? dialog.show(getFragmentManager(), "loginDialog");
?}


效果图:


Android 官方推荐 : DialogFragment 创建对话框


可以看到通过重写onCreateDialog同样可以实现创建对话框,效果还是很nice的。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 屏幕旋转 处理 AsyncTask.. 下一篇Android Fragment 真正的完全解析

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: