简体   繁体   English

保存EditText和TextView数据并将其显示在另一个活动中

[英]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的结果为value1value2


Solution :: Get the values from the views and use intents to pass the data between activities 解决方案 ::从views获取值,并使用intentsactivities之间传递数据


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 使用变量editText1editText2textView1在第二活动为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.

相关问题 如何使用SharedPreferences保存在edittext中输入的文本,并将其显示在另一个Activity中的TextView中 - How to use SharedPreferences to save the text entered in edittext and display it in TextView in another Activity 如何在另一个活动中将editText中的字符串用作显示textview? - How to use the string from editText as display textview in another activity? 在另一个活动中将数据从EditText传递到TextView并通过共享首选项进行使用 - Passing data from EditText to TextView in another activity and savaing it with Shared Preferences 使用setText和onClickListener将数据从EditText传输到另一个活动的TextView吗? - Transfer data from EditText to TextView of another activity using setText and onClickListener? android-使用EditText值到另一个活动的textview - android - Using EditText values to a textview on another activity 编辑后在另一个TextView中显示EditText值 - Display EditText value in another TextView after edit 如何在活动2的textView中的活动1中显示来自editText的文本 - how do i Display text from editText in activity 1 in textView in activity 2, 如何在textview的一个Activity中显示数组中的2个EditText数据? - How can i display 2 EditText data from array in one Activity in textview…? 将EditText和Spinner的值从一个活动传递到另一个活动的TextView中 - Passing values of EditText and Spinner from an Activity into TextView of another Activity 在另一个Activity中显示动态创建的EditText的值 - Display value of dynamically created EditText in another Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM