我有2個活動 1.Register.java 2.SecureXActivity.java 在註冊活動中,我正在使用共享首選項註冊我的應用程序。我已經使用共享偏好來僅顯示註冊活動一次。現在我的要求是從註冊活動取得 用戶名,註冊時只會顯示一次,並將其保存在SecureXActivity活動中,註冊後只有用戶可以看到該活動。所以請在這方面幫助我?我是android新手。如何保存和檢索sharedpreferences中的多個值?
我的代碼下面給出
Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
public static final String USER_NAME = "USER";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0)
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras =getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("name");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(value);
}
}
我想從一個活動到另一個活動和值的值應保存到另一個活動。在我上面的代碼中,我使用意圖完成了這個任務。但問題是價值僅在第二項活動中顯示一次。當我按下返回按鈕或主鍵時,值是不可見的?如何做到這一點。
添加響應@Picarus:
的問題是我使用相同的共享偏好顯示寄存器活動只有一次。 但它沒有顯示在得力名稱activity.Is有可能使用相同的共享的偏好來進行值從寄存器活性得力活動
整個代碼在下面給出再次 Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0)
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", welcome);
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
//intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public class SecureXActivity extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
private ListView m_listview;
Button btnSendlogs;
Button btnMailadmin;
Button btnContactSupport;
Button btnSettings;
@Override
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Here we are setting the installation date of a application
ContextWrapper context = this;
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo;
try
{
appInfo = packageManager.getApplicationInfo("com.simsys.securex", 0);
String sourceDir= appInfo.sourceDir;
long dateTimeSync= new File(sourceDir).lastModified();
String DateTimeStamp=getDate(dateTimeSync, "dd/MM/yyyy hh:mm:ss");
String dateTimeSync2="Installed_Date:";
String dateTimeSync3=dateTimeSync2.concat(DateTimeStamp);
TextView txtCurrentTime= (TextView)findViewById(R.id.textViewdatetime);
txtCurrentTime.setText(dateTimeSync3);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
//Here We Welcome the user
//List View
m_listview = (ListView) findViewById(R.id.list_view);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("CAMERA");
listItems.add("RECORDER");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
m_listview.setAdapter(adapter);
if(isBluetoothServiceRunning())
{
listItems.add("BLUETOOTH");
}
if(isGprsServiceRunning())
{
listItems.add("GPRS");
}
if(isWifiServiceRunning())
{
listItems.add("WIFI");
}
adapter.notifyDataSetChanged();
//Call to Buttons Functionality
btnSendlogs = (Button) findViewById(R.id.button_sendlogs);
btnSendlogs.setOnClickListener(myhandler1);
btnMailadmin=(Button) findViewById(R.id.button_mailtoadmin);
btnMailadmin.setOnClickListener(MailadminHandler);
btnContactSupport = (Button) findViewById(R.id.button_contactsupport);
btnContactSupport.setOnClickListener(ContactSupportHandler);
}
//Navigation from Registration to Main page
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.logout)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String data2=settings.getString("name", "");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(data2);
SharedPreferences.Editor editor = settings.edit();
editor.remove("name");
editor.commit();
finish();
}
return super.onOptionsItemSelected(item);
}
不知道什麼是您的具體疑問檢索。你能澄清嗎?您可以根據需要在「共享首選項」中擁有儘可能多的設置,並且您可以擁有任意數量的不同文件......如果您澄清,我們將能夠爲您提供幫助。 – Picarus 2012-08-01 09:59:11
我希望從一個活動到另一個活動的價值應該保存到另一個活動。在我上面的代碼中,我使用意圖完成了這個任務。但問題是價值僅在第二項活動中顯示一次。當我按下返回按鈕或主頁鍵時,值不可見? – Tirtha 2012-08-01 10:24:13
你面臨的問題是什麼。您正在使用共享首選項,然後嘗試從中獲得。 – Harish 2012-08-01 10:27:01