我正在制作一个申请,其中学生可以输入一次注册,他可以访问该应用程序的其余功能。
我的问题是,如何保存他要输入的注册号,将其保存在变量中并在应用程序的其他活动中使用?
我已经获得了登录活动的代码:
public class MainActivity2 extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
et = (EditText)findViewById(R.id.rfid);
b = (Button)findViewById(R.id.loginnext);
tv = (TextView)findViewById(R.id.tv);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putString("language", et.toString().toString());
editor.commit();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity2.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/check.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(MainActivity2.this, MainActivity3Activity.class));
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
MainActivity2.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
如果我把它放在正确的班级中,我不确定我是否有SharePreferences
权利。
而且,我怎么知道我是否可以将SharedPreferences
用于其他活动?
感谢先进的帮助。
答案 0 :(得分:1)
我不确定我是否将SharePreferences置于其中 合适的。
目前正在SharePreferences
的{{1}}中的EditText中保存值,以保存EditText的默认值。
验证用户后,在onCreate
方法中保存SharePreferences
值:
login()
我怎么知道我是否可以将SharedPreferences用于其他人 活动?
要获取其他活动中的值,请使用if(response.equalsIgnoreCase("User Found")){
//save value here
}
:
getString
答案 1 :(得分:1)
像这样制作共享偏好;
SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
//Now put values in editor
editor.putString(key_value_for_access ,string_value_to_store );
editor.commit();
//现在要将这个访问到每个活动中,你必须写同样的东西:
SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
if(prefernces.contains(key_value_for_access)){
//getting data
String get = preferences.getString(key_value_for_access, null);
}