我想在用户按下警报对话框中的确认后更改 3 个编辑文本的值。但它不刷新。这些值仅在移动到其他片段并返回到该片段后才会改变
SharedPreferences sp;
EditText ip,port,appkey;
Button connect;
public ConnectFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_connect, container, false);
ip= (EditText) v.findViewById(R.id.editid);
port= (EditText) v.findViewById(R.id.editport);
appkey= (EditText) v.findViewById(R.id.editsppKey);
Toast.makeText(getActivity(),"oncreateview",Toast.LENGTH_SHORT).show();
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
connect= (Button) v.findViewById(R.id.connectf);
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
senddata();
}
});
return v;
}
private void senddata() {
//senddata
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(),"on Activity Created",Toast.LENGTH_SHORT).show();
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
}
@Override
public void onAttachFragment(Fragment childFragment) {
super.onAttachFragment(childFragment);
Toast.makeText(getActivity(),"On Attach Fragment",Toast.LENGTH_SHORT).show();
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getActivity(),"On Resume",Toast.LENGTH_SHORT).show();
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
}
@Override
public void onStop() {
super.onStop();
Toast.makeText(getActivity(),"stop",Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
super.onPause();
Toast.makeText(getActivity(),"pause",Toast.LENGTH_SHORT).show();
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible()) {
// If we are becoming invisible, then...
if (!isVisibleToUser) {
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
Log.d("MyFragment", "Not visible anymore. Stopping audio.");
// TODO stop audio playback
}
}
}
@Override
public void onStart() {
super.onStart();
Toast.makeText(getActivity(),"On start",Toast.LENGTH_SHORT).show();
sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE);
ip.setText(sp.getString("ip",null));
port.setText(sp.getString("port",null));
appkey.setText(sp.getString("appkey",null));
}
}
//这是我在活动中的对话框
final View v=LayoutInflater.from(this).inflate(R.layout.formlayout,null);
AlertDialog.Builder ab=new AlertDialog.Builder(this);
ab.setView(v);
tp= (TextInputLayout) v.findViewById(R.id.ip);
tp1= (TextInputLayout) v.findViewById(R.id.port);
tp2= (TextInputLayout) v.findViewById(R.id.appkey);
ab.setTitle("Enter Connection Details");
ab.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String a=tp.getEditText().getText().toString();
String b=tp1.getEditText().getText().toString();
String c=tp2.getEditText().getText().toString();
if(!TextUtils.isEmpty(a)&&!TextUtils.isEmpty(b) && !TextUtils.isEmpty(c))
{
SharedPreferences.Editor edit=sp.edit();
edit.putString("ip",a);
edit.putString("port",b);
edit.putString("appkey",c);
edit.commit();
}
}
});
ab.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog abc=ab.create();
abc.show();
abc.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
}
});