我有一个自定义对话框片段,其中包含2个编辑文本字段,一个用于电子邮件,另一个用于密码。我还为对话框片段创建了一个自定义侦听器,以便将数据传递给另一个活动。问题是没有数据传递,当我记录值时,没有任何数据显示。 R.layout.dialog_signin
是我对话框的自定义布局。
以下是Dialog Fragment的代码:
public class LoginDialogFragment extends DialogFragment {
public interface LoginDialogListener {
public void onDialogSigninClick(DialogFragment dialog);
}
LoginDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (LoginDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement LoginDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Log In");
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
mListener.onDialogSigninClick(LoginDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
以下是我在用户点击"登录"时收听的代码。按钮。我正在使用布局inflater,所以我可以抓取用户在电子邮件和密码字段中键入的字符串,但问题是字符串是空白的,我甚至无法记录该值,它没有' t出现在日志中。
@Override
public void onDialogSigninClick(DialogFragment dialog) {
// User touched the dialog's positive button
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View myView = inflater.inflate(R.layout.dialog_signin, null);
final EditText username = (EditText) myView.findViewById(R.id.username);
final EditText password = (EditText) myView.findViewById(R.id.password);
System.out.println(username.getText()); //this doesn't show up in the log at all
String usernameText = username.getText().toString();
String passwordText = password.getText().toString();
Intent i = new Intent(this, GSSAct.class);
//as a result no data is passed to the activity
i.putExtra("username", usernameText);
i.putExtra("password", passwordText);
startActivity(i);
}
答案 0 :(得分:0)
我解决了我的问题,使其更加混乱;我所做的就是为onDialogSigninClick
方法添加参数。
我的新对话代码:
public class LoginDialogFragment extends DialogFragment {
public interface LoginDialogListener {
public void onDialogSigninClick(DialogFragment dialog, String username, String password);
}
LoginDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (LoginDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement LoginDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View v = inflater.inflate(R.layout.dialog_signin, null);
builder.setTitle("Log In");
final EditText u = (EditText)v.findViewById(R.id.username22);
final EditText p = (EditText)v.findViewById(R.id.password22);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(v)
// Add action buttons
.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
mListener.onDialogSigninClick(LoginDialogFragment.this, u.getText().toString(), p.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
我在片段附加到的活动中的代码:
@Override
public void onDialogSigninClick(DialogFragment dialog, String username, String password) {
// User touched the dialog's positive button
//LayoutInflater inflater = getLayoutInflater();
System.out.println("I am in the dialog.");
//View myView = inflater.inflate(R.layout.dialog_signin, null);
//EditText username2 = (EditText) myView.findViewById(R.id.username22);
//EditText password2 = (EditText) myView.findViewById(R.id.password22);
String usernameText = username;
String passwordText = password;
System.out.println("The username is:" + usernameText);
Intent i = new Intent(this, GSSAct.class);
i.putExtra("username", usernameText);
i.putExtra("password", passwordText);
startActivity(i);
}