如何登錄創建SharedPreferences
與集可視性如果用戶成功登錄該按鈕將被註銷,登錄將會消失如何使用SharedPreferences改變視圖知名度登錄和註銷
public class Login extends ActionBarActivity implements View.OnClickListener{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static String url_check_login = "";
private static final String TAG_SUCCESS = "success";
Context cont=this;
EditText uname, pword;
Boolean nega = false;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_user);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
uname = (EditText) findViewById(R.id.uname);
pword = (EditText) findViewById(R.id.pword);
Button login = (Button)findViewById(R.id.btnLogin);
login.setOnClickListener(this);
Button register = (Button)findViewById(R.id.btnRegister);
register.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
final int id = v.getId();
switch (id) {
case R.id.btnLogin:
new SubmitLogin().execute();
break;
case R.id.btnRegister:
Intent i = new Intent (this, Registration.class);
startActivity(i);
finish();
break;
// even more buttons here
}
}
class SubmitLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Logging In ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", uname.getText().toString()));
params.add(new BasicNameValuePair("pass", pword.getText().toString()));
JSONObject json = jsonParser.makeHttpRequest(url_check_login, "POST", params);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
nega = false;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", uname.getText().toString());
editor.commit();
Intent gotoLogs = new Intent(cont, MainActivity.class);
startActivity(gotoLogs);
finish();
} else {
nega = true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (nega) {
new AlertDialog.Builder(cont)
.setTitle("Login Failed")
.setMessage("Wrong Username or Password")
.setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Close current activity
startActivity(new Intent(this,MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
如果'SharedPreferences'中沒有登錄信息,你是否試圖將按鈕設置爲'gone'? – Ircover
我想創建一個登錄按鈕和註銷按鈕與sharedpreferences和setvisibility – user1201012121