Android填写文本字段

时间:2014-05-20 17:50:47

标签: android login sharedpreferences textfield oncreate

我不知道,哪个因素可能会导致这个问题,因为即使经过调试,仍然无法弄明白。问题是,在应用程序启动后,我尝试使用以前登录时保存的数据填写登录名和密码的文本字段。但是我无法引用TextFields:/它总是得到null,并且不知道为什么。到目前为止我所拥有的是什么。

public class Login extends ActionBarActivity {

private String SHARED_PREFS_KEY = "SBJP";
private String LOGIN_KEY = "login";
private String PASS_KEY = "pass";

private EditText userName;
private EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    PlaceholderFragment phFrag = new PlaceholderFragment();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, phFrag)
                .commit();
    }

    loadPreferences(phFrag);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_login, container, false);
        return rootView;
    }
}

/**
 * Handles login action
 */
public void loginClick(View v){
    this.userName = (EditText) findViewById(R.id.login_screen_tf_login);
    this.password = (EditText) findViewById(R.id.login_screen_tf_password);

    String userNameStr = userName.getText().toString();
    String passStr = password.getText().toString();

    //Log.d("SB", userNameStr+":"+passStr);
    SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
    Editor editor = prefs.edit();

    editor.putString(PASS_KEY, passStr);
    editor.putString(LOGIN_KEY, userNameStr);
    editor.commit();
}

/**
 * Handles creating new account
 */
public void createNewAccountClick(View v){
    Log.d("SB", "Here comes account creation");
}

private void loadPreferences(PlaceholderFragment rootView){
    SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
    String loginStr = prefs.getString(LOGIN_KEY, null);
    String passStr = prefs.getString(PASS_KEY, null);

    this.userName = (EditText) rootView.getView().findViewById(R.id.login_screen_tf_login);
    this.password = (EditText) rootView.getView().findViewById(R.id.login_screen_tf_password);

    Log.d("SB","Login/sn:"+loginStr+passStr);

    if(loginStr != null && userName != null){
        userName.setText(loginStr);
    }
    if(passStr != null && password != null){
        password.setText(passStr);
    }

}

}

有人知道吗,为什么我总是在loadPreferences方法中将userName和password变量设为null?在loginClick方法中,它可以正常工作:/ ...

提前致谢

2 个答案:

答案 0 :(得分:1)

this.userName = (EditText) findViewById(R.id.login_screen_tf_login);
this.password = (EditText) findViewById(R.id.login_screen_tf_password);

在这里,您需要获取片段的根视图,但在PlaceholderFragment类的代码中:

userName = (EditText) getView().findViewById(R.id.login_screen_tf_login);
password = (EditText) getView().findViewById(R.id.login_screen_tf_password);

http://developer.android.com/reference/android/app/Fragment.html#getView()

当然我们建议login_screen_tf_login和login_screen_tf_login EditText-s位于你的fragment_login布局中。

此外:

您可以按照以下方式重新设计PlaceholderFragment:

public static class PlaceholderFragment extends Fragment {

private String login;
private String password;

public void setAuthData(String login, String password){ this.login = login; this.password = password; }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_login, container, false);

    EditText userName = (EditText) rootView.findViewById(R.id.login_screen_tf_login);
    userName.setText(login); 
    EditText passwordET = (EditText) rootView.findViewById(R.id.login_screen_tf_password); 
    passwordET.setText(password);
    return rootView;
}
}

使用PlaceholderFragment类如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
    String login = prefs.getString(LOGIN_KEY, null);
    String password = prefs.getString(PASS_KEY, null);

    PlaceholderFragment phFrag = new PlaceholderFragment();
    phFrag.setAuthData(login, password);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, phFrag)
            .commit();
    }
 }

答案 1 :(得分:0)

由于TextViews位于PlaceholderFragment的布局中,因此它们的逻辑也应该存在。从技术上讲,您可以使用findViewById从父活动中获取片段的视图,但如果可能的话,最好将其保留在片段中,以便您可以拥有多个片段而不会发生ID冲突。 (例如;我知道两种登录表单不太可能。)

所以我要做的第一件事就是将loginClick和loadPreferences移动到PlaceholderFragment中。 (当然,你也可以将PlaceholderFragment重命名为更有用的东西。)一旦移动,你将在onCreateView而不是Activity.onCreate中调用loadPreferences。这应该对你更有意义 - loadPreferences正在填充视图,因此它应该在创建视图时发生。

但是,getView()仍然不能在这里工作。相反,您必须使用在onCreateView的过程中创建的rootView。这是因为getView只能找出从onCreateView返回后的根视图。因此,解决问题的一个好方法是简单地将rootView传递给loadPreferences,并使用rootView.findViewById。

相关问题