背景資料:所有類訪問用戶名:安卓
我開發一個小應用程序。它現在的工作方式是在應用程序啓動的那一刻,提示用戶進行主要活動。然後從那裏,用戶點擊提示登錄活動的登錄按鈕。現在在我的登錄活動中,我設置了一個Intent來獲取用戶的用戶名,然後將其存儲在共享首選項中。如果登錄成功,則用戶去那裏用戶看到mainloggedinpage:[歡迎回來{用戶名}]
的問題是以下內容:
如果用戶經由loginactivity訪問mainloggedinpage那麼你可以很容易地看到他的用戶名,這正是我想要的。但問題是,在主頁面上,我有其他按鈕可用於其他活動。當用戶點擊其中的任何活動並返回主頁面的頁面時,您看到的只是:[Welcome Back {}]。他的用戶名不再顯示。
我的問題:
任何人都可以提出一個速戰速決,將允許當用戶進入到從mainloggedinpage另一個活動,然後回來給mainloggedinpage,用戶我所以即使顯示的用戶名仍然會看到:[Welcome Back {username}]?
我的代碼登錄活動[這是我得到的用戶名]:
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(Login.this, mainpage.class);
i.putExtra("Welcome back, username", username);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
我對mainloggedinActivity [這是顯示用戶名]代碼:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
msuggestions = (Button) findViewById(R.id.btn_suggestions);
msuggestions.setOnClickListener(this);
mprayertimes = (Button)findViewById(R.id.btn_activityone);
mprayertimes.setOnClickListener(this);
mqibladirection =(Button)findViewById(R.id.btn_activitytwo);
mqibladirection.setOnClickListener(this);
mnewsboard = (Button) findViewById(R.id.newsboard);
mnewsboard.setOnClickListener(this);
mhadiths = (Button) findViewById(R.id.btn_activitythree);
mhadiths.setOnClickListener(this);
mchat = (Button) findViewById(R.id.btn_discussion);
mchat.setOnClickListener(this);
mallahnames = (Button) findViewById(R.id.btn_activityfour);
mallahnames.setOnClickListener(this);
// Get Username Start
TextView txt_loggedName = (TextView) findViewById(R.id.txt_loggedName);
Intent intent = getIntent();
String username = intent.getStringExtra("Welcome back, username");
txt_loggedName.setText(username);
// Get Username End
buttonLogout = (Button) findViewById(R.id.logout);
buttonLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
SharedPreferences.Editor editor=mPreferences.edit();
editor.remove("username");
editor.remove("password");
editor.commit();
Message myMessage=new Message();
myMessage.obj="NOTSUCCESS";
handler.sendMessage(myMessage);
finish();
}
});
任何人都可以幫助我當用戶轉到主頁面並單擊其中一個按鈕然後回到主頁面時,它仍然顯示他的用戶名? :)
你太棒了<3字面上!有效。我只有一個小問題。我有完全相同的東西,但唯一的是,而不是「客人」,我已經把:MODE_PRIVATE。你介意解釋一下區別嗎?所以我可以瞭解它背後的原因,並非常感謝你,它的工作就像一個魅力! –
我認爲第二個參數是沒有設置首選項時的默認值。如果用戶名尚未設置,您可能需要做一些不同的事情。 MODE_PRIVATE在這裏並不適用,我認爲它用於權限分配的地方 –