首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将值从活动传递到CustomDialog类

将值从活动传递到CustomDialog类
EN

Stack Overflow用户
提问于 2011-11-19 04:25:07
回答 1查看 1.8K关注 0票数 0

您好,我想将我在活动中通过SharedPreferences保存的值"username“传递给CustomDialog类中的EditText。这意味着我想在我的CustomDialog类中使用EditText.settext (username)。有人能帮我做到这一点吗?

谢谢。

Main活动中的SharedPreference代码:

代码语言:javascript
复制
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();
}
}

现在,我想在我的CustomDialog类中将"USERNM“值设置为EditText:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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();
    }
}
}

在我的主要活动中编辑:

代码语言:javascript
复制
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 
    } 

下面是我显示对话框的方式:

代码语言:javascript
复制
public void getLoginData() throws Exception {

    if (checkInternetConnection() == true){
        MyCustomForm dialog = new MyCustomForm (this, "", "", new OnReadyListener());
        dialog.setContentView(R.layout.inlogdialog);
        dialog.show();       
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-19 04:29:09

传递上下文(以及扩展视图)是一种not recommended。我建议为你的对话创建一个监听器,也许是类似于OnTextChangedListener#onChange(String)的东西。

在您的活动中创建侦听器,并将其传递到您的对话框。然后,当对话框完成时,调用listeners onChange(String)方法,该方法将被设置为向UI线程处理程序触发一个事件,以更新编辑文本。相反,您可以直接传递处理程序。

代码语言:javascript
复制
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);
}

下面是你传递它的方法:

代码语言:javascript
复制
 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
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8188673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档