[英]Save EditText and TextView data and display it in another activity
I'm developing an app which adds two numbers. 我正在开发一个将两个数字相加的应用程序。 The user provides the numbers via EditText and the result is displayed via TextView. 用户通过EditText提供数字,结果通过TextView显示。 What I want to do is to save the values of the numbers entered by the user and the result via button (to see them whenever the user wants) and display them in the layout of another activity (whithout EditText's). 我想要做的是保存用户输入的数字值和通过按钮保存的结果(在用户需要时随时查看它们),并在另一个活动的布局中显示它们(没有EditText)。 Remark that the user would be able to see the results saved whenever he/she wants. 请注意,用户将可以随时查看保存的结果。
Hope you can help me. 希望您能够帮助我。 Thank you so much. 非常感谢。
From Your question what i understand :: 从您的问题中我了解 ::
Your first editText1
has value1
您的第一个editText1
具有value1
Your second editText2
has value2
您的第二个editText2
具有value2
textView1
has the result of value1
and value2
textView1
的结果为value1
和value2
Solution :: Get the values from the views
and use intents
to pass the data between activities
解决方案 ::从views
获取值,并使用intents
在activities
之间传递数据
Code :: 代码 ::
In your current Activity, create a new Intent:
- OnCreate()
In your current Activity, create a new Intent:
OnCreate()
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("editText1",editText1.getText());
i.putExtra("editText2",editText2.getText());
i.putExtra("textView1",textView1.getText());
startActivity(i);
Then in the new Activity, retrieve those values:
- OnCreate()
Then in the new Activity, retrieve those values:
OnCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String editText1= extras.getString("editText1");
String editText2= extras.getString("editText2");
String textView1= extras.getString("textView1");
}
Use the variables editText1
, editText2
and textView1
in second activity to set the values to any views as u wish 使用变量editText1
, editText2
和textView1
在第二活动为u希望的值设置为任何的意见
HOPE THAT HELPS, let me know if you face any problems in debugging 希望能帮助您,如果您在调试中遇到任何问题,请告诉我
make use of SharedPreferences
like this: 利用这样的SharedPreferences
:
SharedPreferences sp= getSharedPreferences("ttt", 0);
SharedPreferences.Editor editor = sp.edit();
String etValue=(EditText)findViewById(R.id.yourEditTextId).getText().toString();
String tvValue=(TextView)findViewById(R.id.yourTextView).getText().toString();
editor.putString("etValue", etValue);
editor.putString("tvValue", tvValue);
editor.commit();
// in anywhere user wants
SharedPreferences settings = getSharedPreferences("ttt", 0);
String yourEditTextValue=settings.getString("etValie", "");
String yourTextViewValue=settings.getString("tvValie", "");
There is multiple ways to achieve that : 有多种方法可以实现:
Method 1: Use static class setter and getter method: 方法1:使用静态类的setter和getter方法:
create static class and set values from first activity and get value from second activity 创建静态类并从第一个活动中设置值并从第二个活动中获取值
Method 2: 方法2:
Post your values through the intent 通过意图发布您的价值观
Method 3: 方法3:
Use database to store data from one activity and get data from other activity 使用数据库存储一项活动的数据并从另一项活动获取数据
Method 4: 方法4:
Use Shared preference 使用共享首选项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.