gpt4 book ai didi

java - 从一项 Activity 转移到另一项 Activity 时如何保留 editText 输入?

转载 作者:行者123 更新时间:2023-12-02 10:03:15 26 4
gpt4 key购买 nike

我的一项 Activity 中有一堆 EditText。它们在屏幕上可见,用户可以在其中输入内容。用户还可以移动到另一个完全不同的屏幕。但是,目前,如果他/她返回到具有 EditText 的 Activity,那么他/她的输入将不再显示在 [第一个] 屏幕上。

我可以使用什么代码将用户输入保留在第一个屏幕/第一个 Activity 的 EditText 中?

提前致谢。

编辑(刚刚更新的代码)

package com.example.owner.introductoryapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class UserDataInputActivity extends AppCompatActivity
{
String[] genderOptions = {"Male", "Female"};

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_user_data_input);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions);
AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue);
acTextView.setThreshold(1);
acTextView.setAdapter(adapter);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("Agestr",(((EditText) findViewById(R.id.AgePromptValue)).getText().toString()));
savedInstanceState.putString("Speedstr",(((EditText) findViewById(R.id.GaitSpeedPromptValue)).getText().toString()));
savedInstanceState.putString("Weightstr",(((EditText) findViewById(R.id.WeightPromptValue)).getText().toString()));
savedInstanceState.putString("Heightstr",(((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
savedInstanceState.putString("Genderstr",(((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
savedInstanceState.putString("UPDRSstr",(((EditText) findViewById(R.id.UPDRSPromptValue)).getText().toString()));
savedInstanceState.putString("TUAGstr",(((EditText) findViewById(R.id.TUAGPromptValue)).getText().toString()));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
((EditText) findViewById(R.id.AgePromptValue)).setText(savedInstanceState.getString("Agestr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.GaitSpeedPromptValue)).setText(savedInstanceState.getString("Speedstr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.WeightPromptValue)).setText(savedInstanceState.getString("Weightstr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.HeightPromptValue)).setText(savedInstanceState.getString("Heightstr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.HeightPromptValue)).setText(savedInstanceState.getString("Genderstr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.UPDRSPromptValue)).setText(savedInstanceState.getString("UPDRSstr"), TextView.BufferType.EDITABLE);
((EditText) findViewById(R.id.TUAGPromptValue)).setText(savedInstanceState.getString("TUAGstr"), TextView.BufferType.EDITABLE);
}

public void onGenericMenuClick(View v)
{
Intent intent = null;

ArrayList<String> inputStrings = new ArrayList<>();
inputStrings.add((((EditText) findViewById(R.id.AgePromptValue)).getText().toString()));
inputStrings.add((((EditText) findViewById(R.id.GaitSpeedPromptValue)).getText().toString()));
inputStrings.add((((EditText) findViewById(R.id.WeightPromptValue)).getText().toString()));
inputStrings.add((((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
if(((EditText) findViewById(R.id.GenderPromptValue)).getText().toString().equals("Male"))
inputStrings.add("1");
else //(((EditText) findViewById(R.id.GenderPromptValue)).getText().toString() == "Female")
inputStrings.add("2");
inputStrings.add((((EditText) findViewById(R.id.UPDRSPromptValue)).getText().toString()));
inputStrings.add((((EditText) findViewById(R.id.TUAGPromptValue)).getText().toString()));

// if you click info buttons (even if the EditTexts are blank) show the Dialogs
if(v.getId() == R.id.gaitInfoButton || v.getId() == R.id.UPDRSInfoButton || v.getId() == R.id.TUAGInfoButton)
{
if(v.getId() == R.id.gaitInfoButton)
{
AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
builder.setMessage(R.string.GaitInformationContent)
.setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {}
});
// Create the AlertDialog object and return it
builder.create().show();
}
if(v.getId() == R.id.UPDRSInfoButton)
{
AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
builder.setMessage(R.string.UPDRSInformationContent)
.setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {}
});
// Create the AlertDialog object and return it
builder.create().show();

}
if(v.getId() == R.id.TUAGInfoButton)
{
AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
builder.setMessage(R.string.TUAGInformationContent)
.setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}
});
// Create the AlertDialog object and return it
builder.create().show();
}
}//If input fields are blank, refresh the activity
else if(inputStrings.get(0).matches("") == true
|| inputStrings.get(1).matches("") == true
|| inputStrings.get(2).matches("") == true
|| inputStrings.get(3).matches("") == true
|| inputStrings.get(4).matches("") == true
|| inputStrings.get(5).matches("") == true
|| inputStrings.get(6).matches("") == true)
{
Toast.makeText(this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, UserDataInputActivity.class));
}//if the input fields are filled and you aren't clicking any info buttons, then navigate to whatever, specified page
else
{
if(v.getId() == R.id.pasttests)
intent = new Intent(this, PastDiagnosticResult.class);
else if (v.getId() == R.id.currenttest)
{
intent = new Intent(this, CurrentDiagnosticResultActivity.class);
intent.putExtra("INPUTVALUE",inputStrings);
}
else if (v.getId() == R.id.myinfo)
intent = new Intent(this, UserDataInputActivity.class);
else if(v.getId() == R.id.VoiceTestingPrompt)
intent = new Intent(this, SpeechRecognition.class);

if (intent != null) {
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.owner.introductoryapplication">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android:permission.RECORD_AUDIO" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomePageActivity" />

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />

<activity android:name=".UserDataInputActivity" />
<activity android:name=".CurrentDiagnosticResultActivity" />
<activity android:name=".PastDiagnosticResult"></activity>
<activity android:name=".SpeechRecognition"></activity>
</application>

</manifest>

最佳答案

您可以使用onSavedInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

来自 How to use onSavedInstanceState example please

关于java - 从一项 Activity 转移到另一项 Activity 时如何保留 editText 输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com