Android 如何使用SharedReferences更改登录和注销中的视图可见性
如何在登录中设置可见性创建Android 如何使用SharedReferences更改登录和注销中的视图可见性,android,Android,如何在登录中设置可见性创建SharedReferences,如果用户成功登录,则按钮将注销,登录将消失 public class Login extends ActionBarActivity implements View.OnClickListener{ private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); private static String url_check_l
SharedReferences
,如果用户成功登录,则按钮将注销,登录将消失
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);
}
}
}
公共类登录扩展ActionBarActivity实现View.OnClickListener{
私人对话;
JSONParser JSONParser=新的JSONParser();
私有静态字符串url_check_login=“”;
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
Context cont=这个;
编辑文本uname,pword;
布尔值nega=false;
专用工具栏;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login\u用户);
工具栏=(工具栏)findViewById(R.id.app\u栏);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
uname=(EditText)findViewById(R.id.uname);
pword=(编辑文本)findviewbyd(R.id.pword);
按钮登录=(按钮)findViewById(R.id.btnLogin);
login.setOnClickListener(this);
按钮寄存器=(按钮)findViewById(R.id.btnRegister);
register.setOnClickListener(this);
}
@凌驾
公共void onClick(视图v)
{
final int id=v.getId();
开关(id){
案例R.id.btnLogin:
新建SubmitLogin().execute();
打破
案例R.id.B登记员:
意向i=新意向(此,Registration.class);
星触觉(i);
完成();
打破
//这里还有更多的按钮
}
}
类SubmitLogin扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
设置消息(“登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
add(新的BasicNameValuePair(“用户名”,uname.getText().toString());
add(新的BasicNameValuePair(“pass”,pword.getText().toString());
JSONObject json=jsonParser.makeHttpRequest(url\u check\u login,“POST”,参数);
d(“创建响应”,json.toString());
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
nega=假;
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
SharedReferences.Editor=prefs.edit();
putString(“用户名”,uname.getText().toString());
commit();
Intent gotoLogs=新的Intent(cont,MainActivity.class);
星触觉;
完成();
}否则{
nega=真;
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
pDialog.disclose();
如果(负){
新建AlertDialog.Builder(续)
.setTitle(“登录失败”)
.setMessage(“错误的用户名或密码”)
.setNegativeButton(android.R.string.yes,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
})
.setIcon(android.R.drawable.ic_对话框_警报)
.show();
}
}
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
//关闭当前活动
startActivity(新意图(this,MainActivity.class));
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
}
要将信息放入共享参考
中,您应该编写以下内容:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
sp.edit().putString(PREFERENCES_LOGIN, login).apply();
变量
首选项\u文件
和首选项\u登录
应定义为字符串
:
public static final String PREFERNCES_FILE = "my_preferences_file";
public static final String PREFERENCES_LOGIN = "login";
注销时,应该有:
sp.edit().remove(PREFERENCES_LOGIN).apply();
@Override
public void onClick(View v)
{
final int id = v.getId();
switch (id) {
case R.id.btnLogin:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
sp.edit().putString(PREFERENCES_LOGIN, login).apply();
findViewById(R.id.btnLogin).setVisibility(View.GONE);
findViewById(R.id.btnLogout).setVisibility(View.VISIBLE);
//another actions after login
break;
case R.id.btnLogout:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
sp.edit().remove(PREFERENCES_LOGIN).apply();
findViewById(R.id.btnLogin).setVisibility(View.VISIBLE);
findViewById(R.id.btnLogout).setVisibility(View.GONE);
//another actions after logout
break;
// even more buttons here
}
}
然后检查是否有allready some info呼叫:
boolean isLogged = sp.contains(PREFERENCES_LOGIN);
然后只需设置所需的可见性:
login.setVisibility(isLogged ? View.GONE : View.VISIBLE);
更新
好的,这里是一个例子。此代码应位于onCreate
中:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
boolean isLogged = sp.contains(PREFERENCES_LOGIN);
Button login = (Button)findViewById(R.id.btnLogin);
login.setVisibility(isLogged ? View.GONE : View.VISIBLE);
login.setOnClickListener(this);
Button logout = (Button)findViewById(R.id.btnLogout);
logout.setVisibility(isLogged ? View.VISIBLE : View.GONE);
logout.setOnClickListener(this);
您的login\u用户
xml中应该有一个带有btnLogout
id的按钮。
在onClick中
应为:
sp.edit().remove(PREFERENCES_LOGIN).apply();
@Override
public void onClick(View v)
{
final int id = v.getId();
switch (id) {
case R.id.btnLogin:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
sp.edit().putString(PREFERENCES_LOGIN, login).apply();
findViewById(R.id.btnLogin).setVisibility(View.GONE);
findViewById(R.id.btnLogout).setVisibility(View.VISIBLE);
//another actions after login
break;
case R.id.btnLogout:
SharedPreferences sp = getSharedPreferences(PREFERNCES_FILE, MODE_PRIVATE);
sp.edit().remove(PREFERENCES_LOGIN).apply();
findViewById(R.id.btnLogin).setVisibility(View.VISIBLE);
findViewById(R.id.btnLogout).setVisibility(View.GONE);
//another actions after logout
break;
// even more buttons here
}
}
在
login
中,变量应该是登录数据-我不知道应该从哪里获取它。如果SharedReferences
中没有登录信息,您是否试图设置gone
按钮的可见性?我正在尝试创建一个登录按钮和注销按钮,其中包含SharedReferences和setvisibilityPreferences\u文件
和PREFERENCES\u LOGIN
是您应该自己定义的变量。public void onLogOutclicked(View v){isLogged=false;loginbtn.setVisibility(View.VISIBLE);logoutbtn.setVisibility(View.GONE);}如何将SharedReference放在这段代码中?LOGIN.setVisibility(isLogged?View.GONE,View.VISIBLE);为什么逗号是错的?哦,我错了<代码>登录.setVisibility(isLogged?View.Goe:View.VISIBLE)