[英]How can I pass an int variable value from one activity to overwrite a textview in another activity
[英]how can I parse a given edittext value from one activity to another? the value should overwrite a given textview in the new activity
我是android編程的新手。 我有一系列的EditText
字段,並且我希望該程序允許用戶在EditText
字段中輸入一個值,並將輸入的任何值都轉移到另一個活動中,該活動中的值將覆蓋textview的內容。 我怎樣才能做到這一點?
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra(AnotherActivity.KEY_EXTRAS_MESSAGE_AUTHOR, this.myEditText.getText().toString()));
startActivity(intent);
在AnotherActivity中:
public class AnotherActivity extends Activity {
public static final String KEY_EXTRAS_MESSAGE_AUTHOR= "author";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
String author = getIntent().getStringExtra(KEY_EXTRAS_MESSAGE_AUTHOR);
您可以如下所示進行操作:
在活動中定義“編輯文本”:
EditText editText = (EditText) findViewById(R.id.editText);
從編輯文本中讀取值:
String readValue = editText.getText().toString();
創建一個Intent並將值傳遞給另一個活動:
Intent intent = new Intent(this, anotherActivity.class);
//Passing the string here
intent.putExtra("value", readValue);
startActivity(intent);
在您的anotherActivity中捕獲意圖並設置值以編輯文本:
Intent intent = getIntent();
String result = intent.getStringExtra("value");
在活動中定義“編輯文本”:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);
如果您不想使用startActvity(intent)來觸發活動,則可以使用以下過程:
將編輯文本值保存在共享首選項中:
在活動中定義“編輯文本”:
EditText editText = (EditText) findViewById(R.id.editText);
從編輯文本中讀取值:
String readValue = editText.getText().toString();
保存值:
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value", readValue);
editor.commit();
在另一個活動中,如下所示使用它
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
String result = sharedPreferences.getString("value");
在活動中定義“編輯文本”:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.