我该怎么做才能在Android中保留`TextView`值?

时间:2017-05-30 12:43:34

标签: android

我的应用主页上有两个文字视图。我有两个按钮启动新活动。我从两个相应的活动中获取文本视图的值,并使用intent将数据传递回主活动。       但是当我这样做时,只显示当前的文本视图值。我该怎么做才能同时显示两者?

MainActivity

definline

Main2Activity

    public class MainActivity extends AppCompatActivity
    {


       private static String 
    MESSAGE_KEY="com.example.kjsce.budgetinyourpocket.message_key";
    private static String MESSAGE_KEY1="com.example.kjsce.budgetinyourpocket.message_key1";
    private TextView textView;
    private  TextView  textView1;

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

        Intent intent=getIntent();
        String message=intent.getStringExtra(MESSAGE_KEY);





        /*String t1=message;
        int t11=Integer.parseInt(t1);
        int t11add=0;
        t11add=t11+t11add;

        String hello=Integer.toString(t11add);*/
        textView=(TextView) findViewById(R.id.expense);
        textView.setText(message);


        Intent intent1=getIntent();
        String message1=intent1.getStringExtra(MESSAGE_KEY1);

        textView1=(TextView) findViewById(R.id.income);
        textView1.setText(message1);





        //textView.setText(hello);
        textView1.setText(message1);
    }




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

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

Main3Activity

 public void send(View view)
{
    amount=(EditText)findViewById(R.id.amt);
    String message=amount.getText().toString();
    Intent i=new Intent(this,MainActivity.class);
       i.putExtra(MESSAGE_KEY,message);
    startActivity(i);

}

`

3 个答案:

答案 0 :(得分:0)

使用共享偏好来设置并从两个活动中获取数据并将其设置为主要活动。

设置偏好

 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","");

答案 1 :(得分:0)

您可以使用startActivityForResult在完成后立即运行触发onActivityResult的活动,以便您可以从其他活动中获取所需的数据

MainActivity.java

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);
        }
    }
}

Activity1.java

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();
    }
}

activity_main.xml中

<?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>

答案 2 :(得分:0)

您应该使用sharePreference 如果您了解sharefreference,请点击developer.android.com或点击www.tutorialspoint.com

我一直在解决你的问题只需完成我的步骤: - )

Strp1: Activity1.xml和activity1.java

    <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();
    }
}
}

Strp2: Activity2.xml和activity2.java

    <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();
        }
    }
}

第3步: main_activity.xml和MainActivity.java

<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();

    }
}

Step4: 最后的menifest.xml

<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>