我应该怎么做才能在Android中保留“TextView”值?

我应该怎么做才能在Android中保留“TextView”值?,android,Android,我的应用程序主页上有两个文本视图。我有两个启动新活动的按钮。我从两个各自的活动中获取文本视图的值,并使用意图将数据传递回主活动。 但执行此操作时,仅显示当前文本视图值。我应该怎么做才能同时显示两者 主要活动 主要活动 维护活动 `对set使用共享首选项,从两个活动中获取数据,并将其设置为main activiy 设置首选项 SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putStri

我的应用程序主页上有两个文本视图。我有两个启动新活动的按钮。我从两个各自的活动中获取文本视图的值,并使用意图将数据传递回主活动。 但执行此操作时,仅显示当前文本视图值。我应该怎么做才能同时显示两者

主要活动 主要活动 维护活动
`

对set使用共享首选项,从两个活动中获取数据,并将其设置为main activiy

设置首选项

 SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("key1", value1);
            editor.putString("key 2", value 2);           
            editor.commit();
获得优先权

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // then you use
  String yourVaue1 =  prefs.getString("Key1","");
  String yourVaue2 =  prefs.getString("Key2","");
您可以使用运行活动,该活动在完成后立即触发,以便您可以从另一个活动获取所需的数据

MainActivity.java Activity1.java activity_main.xml
您应该使用sharePreference 如果您了解sharefreference,请单击或单击

我一直在通过我的步骤来解决你的问题

Strp1:Activity1.xml和Activity1.java

Strp2:Activity2.xml和Activity2.java

步骤3:main_activity.xml和MainActivity.java


第4步:最后一个menifest.xml



请发布一些代码,以便我们可以查看。您尝试过将其放在这里吗?如果您知道onActivityResult,以startActivityForResult而不是startActivity启动新活动。发布所有java代码文件尝试使用SharedReference..它不起作用。现在返回活动时数据不显示。是否检查是否触发了
onActivityResult
方法?是否确实已从子活动返回字符串类型?
        public void sendd(View view)
{
    amount=(EditText)findViewById(R.id.amt);
    String message1=amount.getText().toString();
    Intent i=new Intent(this,MainActivity.class);
    i.putExtra(MESSAGE_KEY1,message1);
    startActivity(i);

}
 SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("key1", value1);
            editor.putString("key 2", value 2);           
            editor.commit();
  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // then you use
  String yourVaue1 =  prefs.getString("Key1","");
  String yourVaue2 =  prefs.getString("Key2","");
public class MainActivity extends AppCompatActivity {

    private static final int ACTIVITY_REQUEST_1 = 1, ACTIVITY_REQUEST_2 = 2;
    TextView textView1, textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 = (TextView) findViewById(R.id.textview1);
        textView2 = (TextView) findViewById(R.id.textview2);
    }

    public void expense(View view) {
        Intent intent = new Intent(this, Activity1.class);
        startActivityForResult(intent, ACTIVITY_REQUEST_1);
    }

    public void income(View view) {
        Intent intent = new Intent(this, Activity2.class);
        startActivityForResult(intent, ACTIVITY_REQUEST_2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ACTIVITY_REQUEST_1) {
            String textValue = data.getStringExtra("textview_value");
            textView1.setText(textValue);

        } else if (requestCode == ACTIVITY_REQUEST_2) {
            String textValue = data.getStringExtra("textview_value");
            textView2.setText(textValue);
        }
    }
}
public class Activity1 extends AppCompatActivity {

    private static final int ACTIVITY_REQUEST_1 = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);

        String textValue = "Something_from_activity_1";
        Intent resultIntent = new Intent();
        resultIntent.putExtra("textview_value", textValue);
        setResult(ACTIVITY_REQUEST_1, resultIntent);
        finish();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.streak.textactivity.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Activity_1_text" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="expense"
            android:text="Launch Activity 1" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Activity_2_text" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="income"
            android:text="Launch Activity 2" />

    </LinearLayout>
</LinearLayout>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ncrypted.demorecyclerviewsubitems.Activity1">

    <EditText
        android:hint="Amount1"
        android:inputType="number"
        android:maxLines="1"
        android:maxLength="5"
        android:id="@+id/etAmount1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:onClick="sendAmount1"
        android:id="@+id/btnSendAmount1"
        android:text="Send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
public class Activity1 extends AppCompatActivity {

private final static String PREF_NAME = "prefName";
private final static String PREF_KEY = "prefKeyActivity1";
SharedPreferences sharedPref;
EditText etAmount1;
Button btnSendAmount1;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);
    sharedPref = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    editor = sharedPref.edit();
    etAmount1 = (EditText) findViewById(R.id.etAmount1);
    btnSendAmount1 = (Button) findViewById(R.id.btnSendAmount1);

}

public void sendAmount1(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    if (etAmount1.getText().toString().equals("")) {
        Toast.makeText(this, "please enter amount!", Toast.LENGTH_SHORT).show();
    } else {
        editor.putString(PREF_KEY, etAmount1.getText().toString().trim());
        editor.commit();
        startActivity(intent);
        finish();
    }
}
}
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ncrypted.demorecyclerviewsubitems.Activity2">

    <EditText
        android:hint="Amount2"
        android:inputType="number"
        android:maxLines="1"
        android:maxLength="5"
        android:id="@+id/etAmount2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:onClick="sendAmount2"
        android:id="@+id/btnSendAmount2"
        android:text="Send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
public class Activity2 extends AppCompatActivity {

    private final static String PREF_NAME = "prefName";
    private final static String PREF_KEY = "prefKeyActivity2";
    SharedPreferences sharedPref;
    EditText etAmount2;
    Button btnSendAmount2;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        sharedPref = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        etAmount2 = (EditText) findViewById(R.id.etAmount2);
        btnSendAmount2 = (Button) findViewById(R.id.btnSendAmount2);
    }

    public void sendAmount2(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        if (etAmount2.getText().toString().equals("")) {
            Toast.makeText(this, "please enter amount!", Toast.LENGTH_SHORT).show();
        } else {
            editor.putString(PREF_KEY, etAmount2.getText().toString().trim());
            editor.commit();
            startActivity(intent);
            finish();
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:orientation="vertical"
    tools:context="com.ncrypted.demorecyclerviewsubitems.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvMain1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Text 1" />

        <TextView
            android:id="@+id/tvMain2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Text 2" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnMain1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="btn1"
            android:onClick="expense"/>

        <Button
            android:onClick="income"
            android:id="@+id/btnMain2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="btn2" />

    </LinearLayout>

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    Button btnOpenActivity1, btnOpenActivity2;
    TextView tvText1, tvText2;
    private final static String PREF_NAME = "prefName";
    private final static String PREF_KEY1 = "prefKeyActivity1";
    private final static String PREF_KEY2 = "prefKeyActivity2";
    SharedPreferences sharedPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPref = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);


        btnOpenActivity1 = (Button) findViewById(R.id.btnMain1);
        btnOpenActivity2 = (Button) findViewById(R.id.btnMain2);
        tvText1 = (TextView) findViewById(R.id.tvMain1);
        tvText2 = (TextView) findViewById(R.id.tvMain2);


    }

    private void getValueFromActivities() {
        if (sharedPref.getString(PREF_KEY1, "").equals("")) {
            tvText1.setText("Value not found!");
        } else {
            tvText1.setText(sharedPref.getString(PREF_KEY1, ""));
        }
        if (sharedPref.getString(PREF_KEY2, "").equals("")) {
            tvText2.setText("Value not found!");
        } else {
            tvText2.setText(sharedPref.getString(PREF_KEY2, ""));
        }
    }

    public void expense(View view) {
        Intent intent = new Intent(this, Activity1.class);
        startActivity(intent);
    }

    public void income(View view) {
        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        getValueFromActivities();

    }
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ncrypted.demorecyclerviewsubitems">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:launchMode="singleTask"
            android:name=".MainActivity"
            android:label="ActivityMain">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity1" android:label="Activity1" />
        <activity android:name=".Activity2" android:label="Activity2"></activity>
    </application>

</manifest>