Android 使用SharedReferences从Edittext检索用户名以用于下一个活动
我想我可以在登录后绕过登录活动,但如何检索用户名并将其传递给下一个活动。我得到用户名的值为空 这里是登录活动Android 使用SharedReferences从Edittext检索用户名以用于下一个活动,android,android-sharedpreferences,Android,Android Sharedpreferences,我想我可以在登录后绕过登录活动,但如何检索用户名并将其传递给下一个活动。我得到用户名的值为空 这里是登录活动 public class LoginActivity extends AppCompatActivity { /* * Database helper field */ private DatabaseHelper databaseHelper; /* Edittext username and password */
public class LoginActivity extends AppCompatActivity {
/*
* Database helper field
*/
private DatabaseHelper databaseHelper;
/*
Edittext username and password
*/
private EditText uName, pwd;
/*
Button login and signup
*/
private Button loginButton, signupButton;
private String username, password;
/**
* Create activity when register button and log in button clicked
* @param savedInstanceState saved instance state
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar)findViewById(R.id.login_toolbar);
toolbar.setTitle("LOGIN");
databaseHelper = new DatabaseHelper(this);
uName = (EditText)findViewById(R.id.uNameToLogin_editText);
pwd = (EditText)findViewById(R.id.pwdToLogin_editText);
SharedPreferences sharedPreferences = getSharedPreferences
(getString(R.string.SHARED_PREFS), MODE_PRIVATE);
boolean isLoggedIn = sharedPreferences.getBoolean(getString(R.string.LOGGEDIN), false);
if (isLoggedIn){
Intent newIntent = new Intent(LoginActivity.this, FinderActivity.class);
newIntent.putExtra("Username", username);
startActivity(newIntent);
finish();
}
/*
* Sign up button clicked takes user to registration form
*/
signupButton = (Button)findViewById(R.id.signupButton);
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
/*
*Log in activity
*/
loginButton = (Button)findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = uName.getText().toString();
password = pwd.getText().toString();
//log in successfull using correct username and password
String pass = databaseHelper.searchPassword(username);
if (pass.equals(password)){
SharedPreferences sharedPreferences = getSharedPreferences
(getString(R.string.SHARED_PREFS), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getString(R.string.LOGGEDIN), true);
editor.commit();
Toast.makeText(LoginActivity.this, "Welcome! " + username,
Toast.LENGTH_LONG).show();
//change to new search intent
Intent newIntent = new Intent(LoginActivity.this, FinderActivity.class);
newIntent.putExtra("Username", username);
startActivity(newIntent);
finish();
} else {
//error shows if inccorrect password or username
Toast.makeText(LoginActivity.this, "Username or Password Incorrect!, Try Again",
Toast.LENGTH_LONG).show();
}
}
});
}
}
这是我的下一个活动,它包含在菜单栏中的注销操作
public class FinderActivity extends AppCompatActivity {
private static final String TAG = "FinderActivity";
private String address = "";
public List<LocationInfo> locationList;
public ListView mListView;
public ArrayAdapter<LocationInfo> locationAdapter;
private int numberOfLocation;
public LocationAddress locationAddress;
public String api_url, uName;
public double lat, lng;
SharedPreferences mSharePreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finder);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
uName = intent.getStringExtra("Username");
String welcomeString = "Welcome, " + uName;
getSupportActionBar().setTitle(welcomeString);
Log.i(TAG, "On create called");
View b = findViewById(R.id.customButton);
b.setVisibility(View.GONE);
/*
retrieve search button actions
*/
Button searchButton = (Button) findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//pass address to location class
EditText search = (EditText) findViewById(R.id.search_EditText);
address = search.getText().toString();
LocationAddress.setAddress(address);
if (locationList != null) {
locationList.clear();
}
if (address.equals("")) {
Toast.makeText(FinderActivity.this, "PLease enter your address!",
Toast.LENGTH_LONG).show();
} else {
/*
* manging connection from the application to networking service
* before attempting to fetch url, make sure there is a network connection
*/
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networtInfo = connectivityManager.getActiveNetworkInfo();
if (networtInfo != null && networtInfo.isConnected()) {
GetAddressTask task = new GetAddressTask();
task.execute();
View b = findViewById(R.id.customButton);
b.setVisibility(View.GONE);
Log.i(TAG, "connected");
} else {
Log.i(TAG, "not connected");
}
/*
Retrieve data to listview
*/
showListView();
/*
itemClick listener for each item in the list view
*/
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(FinderActivity.this, ParkingLocation.class);
api_url = locationList.get(position).getUrl_api();
intent.putExtra("key_api_url", api_url);
intent.putExtra("username", uName);
startActivity(intent);
}
});
}
}
});
/*
Custom search button actions if and only if search button gives null results
*/
Button customSearch = (Button) findViewById(R.id.customButton);
customSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (address.equals("")) {
Toast.makeText(FinderActivity.this, "PLease enter your address!",
Toast.LENGTH_LONG).show();
} else if (LocationAddress.getNumOfLocations() > 0) {
Toast.makeText(FinderActivity.this, "This action is only available if no parking locations found!!",
Toast.LENGTH_LONG).show();
} else { //open custom search activity
Intent newIntent = new Intent(FinderActivity.this, CustomSearchActivity.class);
Bundle b = new Bundle();
b.putDouble("key_lat", lat);
b.putDouble("key_lng", lng);
b.putString("username", uName);
newIntent.putExtras(b);
startActivity(newIntent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
//log out action
case R.id.action_logout:
mSharePreferences = getSharedPreferences(getString
(R.string.SHARED_PREFS), MODE_PRIVATE);
SharedPreferences.Editor editor = mSharePreferences.edit();
editor.putBoolean(getString(R.string.LOGGEDIN), false);
editor.commit();
Intent backIntent = new Intent(FinderActivity.this, LoginActivity.class);
startActivity(backIntent);
return true;
//show my saved places view
case R.id.action_show:
Intent intent = new Intent(FinderActivity.this, MyDestination.class);
intent.putExtra("username", uName);
startActivity(intent);
return true;
}
//show list of destination once this button clicked
return super.onOptionsItemSelected(item);
}
公共类FinderActivity扩展了AppCompative活动{
私有静态最终字符串标记=“FinderActivity”;
私有字符串地址=”;
公共列表位置列表;
公共列表视图;
公共阵列适配器位置适配器;
私人国际电话号码;
公共地址;
公共字符串api_url,uName;
公共双lat,液化天然气;
共享引用mSharePreferences;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u finder);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
Intent=getIntent();
uName=intent.getStringExtra(“用户名”);
String-welcomeString=“欢迎,”+uName;
getSupportActionBar().setTitle(welcomeString);
Log.i(标记“On create called”);
视图b=findViewById(R.id.customButton);
b、 设置可见性(View.GONE);
/*
检索搜索按钮操作
*/
按钮搜索按钮=(按钮)findViewById(R.id.search_按钮);
searchButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//将地址传递给location类
EditText搜索=(EditText)findViewById(R.id.search\u EditText);
地址=search.getText().toString();
LocationAddress.setAddress(地址);
if(locationList!=null){
locationList.clear();
}
if(地址等于(“”){
Toast.makeText(FinderActivity.this,“请输入您的地址!”,
Toast.LENGTH_LONG).show();
}否则{
/*
*管理从应用程序到网络服务的连接
*在尝试获取url之前,请确保存在网络连接
*/
ConnectivityManager ConnectivityManager=(ConnectivityManager)
getSystemService(Context.CONNECTIVITY\u服务);
NetworkInfo netortinfo=connectivityManager.getActiveNetworkInfo();
if(networtInfo!=null&&networtInfo.isConnected()){
GetAddressTask=新建GetAddressTask();
task.execute();
视图b=findViewById(R.id.customButton);
b、 设置可见性(View.GONE);
Log.i(标记“已连接”);
}否则{
Log.i(标签“未连接”);
}
/*
将数据检索到listview
*/
showListView();
/*
项目单击列表视图中每个项目的侦听器
*/
mListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
意向意向=新意向(FinderActivity.this,ParkingLocation.class);
api_url=locationList.get(position.getUrl_api();
intent.putExtra(“key\u api\u url”,api\u url);
intent.putExtra(“用户名”,uName);
星触觉(意向);
}
});
}
}
});
/*
自定义搜索按钮操作当且仅当搜索按钮给出空结果时
*/
按钮customSearch=(按钮)findViewById(R.id.customButton);
customSearch.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(地址等于(“”){
Toast.makeText(FinderActivity.this,“请输入您的地址!”,
Toast.LENGTH_LONG).show();
}else if(LocationAddress.getNumOfLocations()>0){
Toast.makeText(FinderActivity.this),“此操作仅在未找到停车位置时可用!!”,
Toast.LENGTH_LONG).show();
}else{//打开自定义搜索活动
Intent newIntent=newIntent(FinderActivity.this、CustomSearchActivity.class);
Bundle b=新Bundle();
b、 putDouble(“钥匙”,lat);
b、 putDouble(“键_lng”,lng);
b、 putString(“用户名”,uName);
新意图.额外支出(b);
星触觉(新意图);
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
开关(id){
//注销操作
案例R.id.行动\注销:
mSharePreferences=getSharedPreferences(getString
(R.string.SHARED\u PREFS),MODE\u PRIVATE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
if (getIntent() != null) {
final Bundle extras = getIntent().getExtras();
if (extras != null) {
userName = extras.getString("Username");
}
}
}