个人资料片段 这是我尝试实现自定义对话框
的片段类 public class profileFragment extends Fragment implements
dialog.Dialoglistner {
String path;
Uri imageUri;
ImageView img;
ImageButton save;
private static final int PICK_IMAGE = 100;
TextInputEditText email, add, pswd, phone, name;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_profile,
container, false);
pswd = (TextInputEditText) view.findViewById(R.id.prof_pswd);
phone = (TextInputEditText) view.findViewById(R.id.prof_phone);
img = (ImageView) view.findViewById(R.id.profile);
save = (ImageButton) view.findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
savedata();
}
});
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});
pswd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//calling dialog class
dialog dialogbox = new dialog();
dialogbox.show(getActivity().getSupportFragmentManager(), "Custom Dialog");
}
});
return view;
}
@Override
public void applyText(String s1, String s2) {
pswd.setText(s2);
}
}
对话框类
public class dialog extends AppCompatDialogFragment {
private Dialoglistner Listner;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final EditText old, newpass, repass;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_layout, null);
old = (EditText) view.findViewById(R.id.OldPass);
newpass = (EditText) view.findViewById(R.id.NewPass);
repass = (EditText) view.findViewById(R.id.RePass);
builder.setView(view)
.setTitle("Change Password")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String s1, s2, s3;
s1 = old.getText().toString();
s2 = newpass.getText().toString();
s3 = repass.getText().toString();
if (s2.compareTo(s3) == 0) {
Listner.applyText(s1, s2);
Toast toast = Toast.makeText(getActivity(), "Password Updated", Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(getActivity(),
" Password did not match, please try again", Toast.LENGTH_SHORT);
toast.show();
}
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
此行中的错误
com.example.rachitshah.volunteer.Main2Activity无法强制转换为 com.example.rachitshah.volunteer.dialog $ Dialoglistner 在com.example.rachitshah.volunteer.dialog.onAttach(dialog.java:70)
Listner =(Dialoglistner)上下文; }
public interface Dialoglistner {
void applyText(String s1, String s2);
}
}
答案 0 :(得分:0)
为什么要创建对话框类, 删除这些:
dialog dialogbox = new dialog();
dialogbox.show(getActivity().getSupportFragmentManager(), "Custom Dialog");
和对话框类以及此
implements
dialog.Dialoglistner
只需使用此:
final EditText old, newpass, repass;
Dialog dialog=new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_layout);
old = (EditText) dialog.findViewById(R.id.OldPass);
newpass = (EditText) dialog.findViewById(R.id.NewPass);
repass = (EditText) dialog.findViewById(R.id.RePass);
Button btnChangePassword=dialog.findViewById(R.id.btnChangePassword);
btnChangePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s1, s2, s3;
s1 = old.getText().toString();
s2 = newpass.getText().toString();
s3 = repass.getText().toString();
if (s2.compareTo(s3) == 0) {
//Listner.applyText(s1, s2);
Toast toast = Toast.makeText(getActivity(), "Password Updated", Toast.LENGTH_SHORT);
toast.show();
} else {
Toast toast = Toast.makeText(getActivity(),
" Password did not match, please try again", Toast.LENGTH_SHORT);
toast.show();
}
}
});
dialog.show();