[英]android pass the username and password in edittext of another activity
我正在使用登錄開發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));
這是應該傳遞數據的下一個活動。
使用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");
要將數據從onr活動傳遞到其他活動,可以使用意圖或共享首選項。
要在編輯文本框中將密碼設置為不顯示字符,請執行以下操作 :將此android:inputType="phone"
設置為第二個活動的編輯框,在該活動中您將在Xml布局中顯示密碼, 或者從您的活動中也可以將其動態設置為userPassword.setInputType(129)
(它將文本輸入密碼)。希望有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.