【发布时间】:2021-06-21 15:17:45
【问题描述】:
我正在尝试制作一个应用程序,其中用户输入文本并按下回车键,并且按钮动态放置在不同的活动上,其文本设置为用户输入的内容。我不知道该怎么做,我正在考虑将用户输入保存在 sharedPreference 中,但我不确定从那里去哪里。
package com.example.opisa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class AddGoalsActivity extends AppCompatActivity {
// Create object of SharedPreferences.
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
//now get Editor
SharedPreferences.Editor editor= sharedPref.edit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_goals);
}
public void addGoalText(View view) {
EditText name = (EditText) findViewById(R.id.editText1);
TextView mText = (TextView) findViewById(R.id.textView);
if (name.getText().toString().isEmpty()) {
mText.setText("Didnt type anything");
} else {
mText.setText(name.getText().toString());
}
}
//put your value
editor.putString("goals", name);
//commits your edits
editor.commit();
}
在用户输入他想要的内容后,我希望 mText 成为按钮上的文本值,该按钮会动态出现在不同的活动上。
【问题讨论】:
标签: android button dynamic android-edittext sharedpreferences