http://blog.csdn.net/lmj623565791/article/details/37815413
使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。
onCreateView即使用定义的xml布局文件展示Dialog。
onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
方法一:重写onCreateView创建Dialog
a)新建一个布局文件dialog.xml 作为要现实的对话框
<?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" />
</RelativeLayout>
b)继承DialogFragment类,重写onCreateView方法
public class Dialogfr extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog, container);
return view;
}
}
c)测试运行:Main方法中调用:
android.app.FragmentManager fragmentManager = getFragmentManager();
dialog.show(getFragmentManager(), "loginDialog");
不过默认对话框有个讨厌的标题,我们怎么去掉呢:
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
方法二: 重写onCreateDialog创建Dialog
重写onCreateDialog. 一般可以使用AlertDialog或者Dialog创建对话框。
首先脑补下AlertDialog的用法,AlertDialog的构造方法都是Protected,
所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
接着,必须了解AlertDialog.Builder的几个用法。
(关于AlertDialog的详解,查看Android AlertDialog 详解 )
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自定义样式
setItems :设置对话框要显示的一个list,一般用于显示几个命令时
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton :普通按钮
setPositiveButton :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮
create : 创建对话框
show :显示对话框
在onCreateDialog中一般可以使用AlertDialog或者Dialog创建对话框,
不过既然google不推荐直接使用Dialog,我们就使用AlertDialog来创建一个登录的对话框。
A)创建布局文件
<?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>
public class DialogFrag 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.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).setNeutralButton("详细",null);
return builder.create();
}
}
C)主函数调用
DialogFrag dialog = new DialogFrag();
dialog.show(getFragmentManager(), "loginDialog");
密码应该显示为****************,并且把登陆的用户名密码返回Activity
首先在其内部声明一个接口。
public class LoginDialogFragment extends DialogFragment {
public interface LoginInputListener {
void onLoginInputComplete(String username, String password);
}
}
然后在相应的方法是执行这一段:
LoginInputListener listener = (LoginInputListener) getActivity();
listener.onLoginInputComplete(mUsername.getText().toString(), mPassword .getText().toString());
最后在MainActivity中需要实现我们的接口LoginInputListener,实现我们的方法。
public class MainActivity extends Activity implements LoginInputListener {
@Override
public void onLoginInputComplete(String username, String password) {
Toast.makeText(this, "帐号:" + username + ", 密码 :" + password,
Toast.LENGTH_SHORT).show();
}
}
public class DialogFrag 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.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();
}
}
public class MainActivity extends AppCompatActivity implements DialogFrag.LoginInputListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DialogFrag dialog = new DialogFrag();
dialog.show(getFragmentManager(), "loginDialog");
}
@Override
public void onLoginInputComplete(String username, String password) {
Toast.makeText(this, "帐号:" + username + ", 密码 :" + password,
Toast.LENGTH_SHORT).show();
}
}