android在另一个活动的edittext中传递用户名和密码

时间:2014-02-04 11:46:25

标签: android android-edittext

我正在使用登录开发Android应用程序。当用户登录时,我想将用户名和密码都传递给另一个edittext中的另一个活动。传递的用户名和密码应显示在另一个活动的另一个edittext中。密码应该看起来像(不显示字符)。我知道这可能听起来很奇怪,但这可能吗?只要给我一个答案,我知道你可能会问我为什么要把这个东西展示给另一个edittext但我有自己的目的。

这是我需要传递给另一个edittext

inputusername = (EditText) findViewById(R.id.loginUsername);
inputPassword = (EditText) findViewById(R.id.loginPasswod);

imgLogin1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                String username = inputusername.getText().toString();
                String password = inputPassword.getText().toString();
                UserFunctions userFunction = new UserFunctions();
                Log.d("Button", "Login");
                JSONObject json = userFunction.loginUser(username, password);

                // check for login response
                try {
                    if (json.getString(KEY_PSUCCESS) != null) {
                        loginErrorMsg.setText("");
                        String res = json.getString(KEY_PSUCCESS); 
                        if(Integer.parseInt(res) == 1){
                            // user successfully logged in
                            // Store user details in SQLite Database
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");

                            // Clear all previous data in database
                            userFunction.logoutUser(getApplicationContext());
                            db.addUser(json_user.getString(KEY_PUSERNAME), json.getString(KEY_PUID), json_user.getString(KEY_PCREATED_AT));                     

                            // Launch Dashboard Screen
                            Intent dashboard = new Intent(getApplicationContext(), PatientHomeActivity.class);
                            dashboard.putExtra(KEY_PUSERNAME,username);

                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);

                            // Close Login Screen
                            finish();
                        }else{
                            // Error in login
                            loginErrorMsg.setText("Incorrect username/password");
                    }

修改 这是我传递数据的地方

 private static final String TAG_PUSERNAME = "username";
 private static final String TAG_PASSWORD = "password";

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        /*
         * Start and bind the  imService 
         **/
        startService(new Intent(Login.this,  IMService.class));         


        setContentView(R.layout.login_screen);
        setTitle("Login");
        Bundle i = getIntent().getExtras();

        Button loginButton = (Button) findViewById(R.id.login);
        cancelButton = (Button) findViewById(R.id.cancel_login);
        usernameText = (EditText) findViewById(R.id.userName);
        usernameText.setText(i.getString(TAG_PUSERNAME));
        passwordText = (EditText) findViewById(R.id.password); 
        passwordText.setText(i.getString(TAG_PASSWORD));

这是下一个应该传递数据的活动。

2 个答案:

答案 0 :(得分:4)

使用Bundle将值从当前活动传递到下一个活动

传递数据的当前活动

Intent i = new Intent (BundleActivity.this,datapass.class);
Bundle b =new Bundle();
b.putString("Username","your Username editext value");
b.putString("Password","your Password editext value");
i.putExtras(b);
startActivity(i);

在接收数据的下一个活动中

Bundle b = getIntent().getExtras();
String Username= b.getString("Username");
String Password= b.getString("Password");

答案 1 :(得分:3)

要将数据从onr活动传递给其他人,您可以使用意图或共享首选项。

在编辑文本框中将密码设置为非显示字符:将此android:inputType="phone"设置为您在Xml布局中显示密码的第二个活动的编辑框从您的活动中,您也可以将其动态设置为userPassword.setInputType(129)(它会将文本设置为密码)。希望它会有所帮助。