所以我做了兩個文本編輯。當你點擊"submit"時,我想把它作為TextView轉移到另一個活動。我已經設法做到了,但唯一的問題是,當我去另一個活動并重新訪問這個活動時,那些我用"submit"按鈕改變它的文本視圖就會丟失。它只是顯示"null"。有誰能幫我解決這個問題。這樣它就可以停留在那里而不丟失資料。以下是我的java1Activity代碼:
public class TwoTeam extends AppCompatActivity {
編輯文本 first_name, second_name;
字串name_first, name_second。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.activity_two_team)。
first_name = findViewById(R.id.first_name);
second_name = findViewById(R.id.second_name)。
}
public void submit_button(View view) {
Intent intent = new Intent(二隊。 this, Begin.class);{
name_first = first_name.getText().toString()。
name_second = second_name.getText().toString()。
intent.putExtra("name1", name_first)。
intent.putExtra("name2", name_second)。
startActivity(intent);
完成()。
}
}
}
這是2Activity代碼:
public class Begin extends AppCompatActivity {
private TextView first_name, second_name;
字串name_first, name_second。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.activity_begin)。
first_name = findViewById(R.id.name_first);
second_name = findViewById(R.id.name_second);
name_first = getIntent().getExtras().getString("name1")。
first_name.setText(name_first)。
name_second = getIntent().getExtras().getString("name2")。
second_name.setText(name_second)。
}
uj5u.com熱心網友回復:
為什么要在startActivity之后呼叫finish()方法?如果你使用這個方法,這個類將在Begin類開始后被殺死,所以你會失去TwoTeam類的資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327617.html
標籤:
