您好我想将我通过活动中的SharedPreferences保存的值“username”传递给CustomDialog类中的EditText。
这意味着我想在我的CustomDialog类中使用EditText.settext (username)
。
有人可以帮助我实现这个目标吗?
谢谢。
主要活动中的SharedPreference代码:
public class AndroidLogin extends Activity implements OnClickListener {
public SharedPreferences mPrefs;
...
@Override
protected void onPause() {
super.onPause();
Editor e = mPrefs.edit();
e.putString(USERNM, username);
e.commit();
}
}
现在我想将值“USERNM”设置为CustomDialog类中的EditText:
public class MyCustomForm extends Dialog {
public SharedPreferences mPrefs;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
EditText userTest = (EditText)findViewById(R.id.txtUserName);
mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE); // does not work
String text = mPrefs.getString("USERNM", "");
userTest.setText(text);
}
}
CustomDialog:
import android.app.Dialog;
...
public class MyCustomForm extends Dialog {
String mTextChange;
public SharedPreferences mPrefs;
public interface ReadyListener {
public void ready(String user, String pass, boolean save);
}
private String usernm;
private String passwd;
private ReadyListener readyListener;
public MyCustomForm(Context context, String user, String pass, ReadyListener readyListener) {
super(context);
this.usernm = user;
this.passwd = pass;
this.readyListener = readyListener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.sencide.R.layout.inlogdialog);
setTitle("Login:");
EditText userTest = (EditText)findViewById(R.id.txtUserName);
//mPrefs = getSharedPreferences("PREFS_NAME", this.MODE_PRIVATE);
//String text = mPrefs.getString("USERNM", "");
userTest.setText();
Button buttonConfrim = (Button) findViewById(R.id.btnConfirm);
buttonConfrim.setOnClickListener(new OKListener());
Button buttonCancel = (Button) findViewById(R.id.btnCancel);
buttonCancel.setOnClickListener(new CancelListener());
}
private class OKListener implements android.view.View.OnClickListener {
public void onClick(View v) {
EditText user = (EditText) findViewById(R.id.txtUserName);
EditText pass = (EditText) findViewById(R.id.txtpass);
CheckBox saveuser = (CheckBox) findViewById(R.id.saveuser);
readyListener.ready(user.getText().toString(),pass.getText().toString(), saveuser.isChecked());
MyCustomForm.this.dismiss();
}
}
private class CancelListener implements android.view.View.OnClickListener {
public void onClick(View v) {
MyCustomForm.this.dismiss();
}
}
}
编辑,在我的主要活动中:
public void createDialog(Context context) {
SharedPreferences prefs = context.getSharedPreferences("mPrefs", Context.MODE_PRIVATE);
String user = prefs.getString("USERNM", "");
new MyCustomForm(context, user, user, null); // where the class that is calling this is aa OnTextChangeListener
}
这是我展示对话框的方式:
public void getLoginData() throws Exception {
if (checkInternetConnection() == true){
MyCustomForm dialog = new MyCustomForm (this, "", "", new OnReadyListener());
dialog.setContentView(R.layout.inlogdialog);
dialog.show();
}
答案 0 :(得分:1)
它not recommended传递上下文(以及扩展视图)。我建议为你的对话创建一个监听器,可能像OnTextChangedListener#onChange(String)
。
在您的活动中创建监听器并将其传递给您的对话框。然后在对话框完成后,调用侦听器onChange(String)
方法,该方法将被设置为向UI线程处理程序触发事件以更新编辑文本。相反,你可以通过处理程序。
public class MyDialog exteds Dialog implements Dialog.OnClickListener {
OnTextChangeListener mListener;
String mTextChange;
public MyDialog(OnTextChangeListener listener) {
mListener = listener;
// set up your stuff
setOnClickListener(this);
}
...
public void onClick(DialogInterface dialog) {
// Do what ever you need to do to get set mTextChange
mListener.onChange(mTextChange);
}
public static interface OnTextChangeListener {
void onChange(String textChange);
}
以下是传递方式:
public void createDialog(Context context) {
SharedPreferences prefs = context.getSharedPreferences("MyPreferencesName", Context.MODE_PRIVATE);
String user = prefs.getString("what_ever_string_you_want", "fall_back_user_name");
new MyDialog(user, this); // where the class that is calling this is aa OnTextChangeListener
}