用户在另一个类中输入后,在TextView中显示字符串

时间:2019-03-28 07:36:56

标签: android textview

我试图将字符串从类(UserInput)传递给Main Activity并将其显示在TextView中。用户应该在Main Activity中按下一个按钮,该按钮调用UserInput并输入一个String。使用共享首选项,我设法将字符串传递过来,但是无法使其显示在TextView中。

我意识到getSharedPreference和setText部分是在类开始之前完成的,导致从UserInput返回后TextView无法更新,我也不知道如何解决此问题。任何帮助都感激不尽。谢谢!

主要活动代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvCode;
Button btnCode, btnClear;
TextInputLayout textInputMessage;
String preferenceFileName, preferenceKey, retreivedCodeString;

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

    tvCode = findViewById(R.id.textViewCode);
    btnCode = findViewById(R.id.buttonCode);
    btnClear = findViewById(R.id.buttonClear);
    textInputMessage = findViewById(R.id.textInputLayoutMessage);
    preferenceFileName = "PreferenceFile";
    preferenceKey = "CodeKey";


    SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
    retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
    tvCode.setText(retreivedCodeString);

    btnCode.setOnClickListener(this);
    btnClear.setOnClickListener(this);
}


@Override
public void onClick(View arg0) {
    switch (arg0.getId()){
        case R.id.buttonClear:
            //method
            break;
        case R.id.buttonCode:
            Intent intent = new Intent(this, UserInput.class);
            startActivity(intent);
            sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
            retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
            tvCode.setText(retreivedCodeString);
            break;
    }

}

UserInput代码

public class UserInput extends Activity implements View.OnClickListener {
String userInputCode, preferenceFileName, preferenceKey;
TextInputLayout subTextInputLayoutCode;

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

    subTextInputLayoutCode = findViewById(R.id.textInputLayoutCode);
    Button btnOk = findViewById(R.id.buttonOk);
    Button btnCancel = findViewById(R.id.buttonCancel);
    preferenceFileName = "PreferenceFile";
    preferenceKey = "CodeKey";

    btnOk.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    switch (arg0.getId()){
        case R.id.buttonOk:
            if (!validateCode()){
                return;
            }
            userInputCode = subTextInputLayoutCode.getEditText().getText().toString();
            SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(preferenceKey,userInputCode);
            editor.apply();
            finish();
            break;
        case R.id.buttonCancel:
            finish();
            break;
    }
}

1 个答案:

答案 0 :(得分:2)

方法1:

onResume的{​​{1}}方法中使用以下代码

MainActivity

方法2:

开始您的SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE); retreivedCodeString = sharedPreferences.getString(preferenceKey,""); tvCode.setText(retreivedCodeString); 活动以获取结果(UserInput

并使用以下代码完成startActivitForResult(intent, 123)活动

UserInput

在您的主要活动中覆盖setResult(Activity.RESULT_OK) finish() ,如下所示

onActivityResultMethode

方法3 :(无sharedPreferences)

开始您的override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if(requestCode == 123 && resultCode == Activity.RESULT_OK){ SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE); retreivedCodeString = sharedPreferences.getString(preferenceKey,""); tvCode.setText(retreivedCodeString); } else { super.onActivityResult(requestCode, resultCode, data) } } 活动以获取结果(UserInput

并使用以下代码完成startActivitForResult(intent, 123)活动

UserInput

在您的主要活动中覆盖Intent i = new Intent(); Bundle b = new Bundle(); b.putString("inputText", YOUR_INPUT_TEXT); i.putExtras(b); setResult(123, i) finish() ,如下所示

onActivityResultMethode