eclipse 如何将用户输入的数据保存到编辑文本中,以便在同一应用程序内的另一个活动中查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19107803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save data a user inputs into an edittext to be viewed in another activity within the same app
提问by Wesley Franks
I am trying to save the data that has been entered by the user from an edittext field. To then be saved for later so that is can be accessed later in another activity within the same app.
我正在尝试保存用户从编辑文本字段输入的数据。然后保存以备后用,以便稍后在同一应用程序中的另一个活动中访问。
I am basically asking the user to input first
, last name
, phone number
, email
, address
.
我基本上是要求用户输入first
, last name
, phone number
, email
, address
。
I want to save that information so it can be viewed in another activity. I have another question regarding that activity but I want to stick to one thing at a time.
我想保存该信息,以便可以在另一个活动中查看。我有另一个关于该活动的问题,但我想一次只做一件事。
package com.internal.companyapp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class NewCustomer extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_customer);
}
public void FirstName(View v)
{
EditText firstname = (EditText) findViewById(R.id.edit_first_name);
firstname.getText().toString();
}
public void LastName(View v)
{
EditText lastname = (EditText) findViewById(R.id.edit_last_name);
lastname.getText().toString();
}
public void SaveCustomer(View v)
{
Saveinfo(v);
toast_newcus(v);
}
public void toast_newcus(View v)
{
EditText firstname = (EditText) findViewById(R.id.edit_first_name);
EditText lastname = (EditText) findViewById(R.id.edit_last_name);
Toast.makeText(this, firstname.getText() + " " + lastname.getText() + " is saved!" .toString(), Toast.LENGTH_LONG).show();
}
public void Saveinfo(View v)
{
}
}
When the info is entered the button is pressed the first and last name are brought into the toast to show that is has been saved. Of course its there for functionality.
当输入信息时,按下按钮,名字和姓氏被带入 toast 以显示已保存。当然它的功能。
How would I then save the data so I can recall it in the viewcustomer
activity. I essentially want to be able to search for a customer then click on the name and have it take me to another activity that will allow me to just view the information.
然后我将如何保存数据,以便我可以在viewcustomer
活动中调用它。我本质上希望能够搜索客户,然后单击该名称,然后将其带到另一项活动,以便我仅查看信息。
Right now I want to focus on just saving the information then if I can't figure out how to recall the information I will post another question. I want to be able to save the information to a file that will be on the internal sdcard
.
现在我只想专注于保存信息,然后如果我不知道如何回忆这些信息,我会发布另一个问题。我希望能够将信息保存到内部sdcard
.
回答by Maxim Efimov
Use SharedPreferencesfor storing any primitive data.
使用SharedPreferences存储任何原始数据。
回答by Piyush
You can refer the below article for SharedPreferences
您可以参考以下文章了解 SharedPreferences
http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/
http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/
回答by Bhargav Vasani
If you want to save data until your application is active or your application not kill than you can also user Applicationclass of android. And generate getter and setter method there for storing and retrieving data.
如果您想保存数据直到您的应用程序处于活动状态或您的应用程序没有被杀死,那么您也可以使用 android 的Application类。并在那里生成用于存储和检索数据的 getter 和 setter 方法。
回答by UserOnE
Define your variable "private static String" so it can save for till the app is running!!
定义您的变量“私有静态字符串”,以便它可以保存到应用程序运行为止!!