英文:
How to get string value from Onclick() to onSaveInstanceState() in Android Studio
问题
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button buttonshow;
EditText edittextname;
TextView textviewshow;
String etText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
etText = edittextname.getText().toString();
if (v.getId() == R.id.buttonShow)
{
textviewshow.setText(etText);
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString("textname", etText);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
etText = savedInstanceState.getString("textname");
}
}
我正在学习如何使用onSaveInstanceState()
和onRestoreInstanceState()
。这样我就可以在活动转为横向模式时恢复字符串。但我无法从Onclick()
方法中获取值传递给其他方法。我需要将etText
的字符串值传递给onSaveInstanceState()
和onRestoreInstanceState()
方法。否则,这些方法无法正常工作。在活动转为横向模式时,我无法恢复这个字符串值。
英文:
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button buttonshow;
EditText edittextname;
TextView textviewshow;
String etText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
etText = edittextname.getText().toString();
if (v.getId() == R.id.buttonShow)
{
textviewshow.setText(etText);
}
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putString("textname", etText);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
etText = savedInstanceState.getString("textname");
}
}
I am learning use of onSaveInstanceState()
and onRestoreInstanceState()
. So I can restore string when my activity turned into the landscape. But I can't get value from Onclick()
to other methods. I need the string value of etText
to onSaveInstanceState()
and onRestoreInstanceState()
. Otherwise, these methods don't work. I can't restore this string value when activity turned to landscape.
答案1
得分: 3
请看以下代码:
public class MainActivity extends AppCompatActivity {
private final String TEXT_KEY = "TEXT_KEY";
Button buttonshow;
EditText edittextname;
TextView textviewshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "onCreate");
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textviewshow.setText(edittextname.getText().toString());
}
});
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("MyTag", "onSaveInstanceState");
outState.putString(TEXT_KEY, textviewshow.getText().toString());
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d("MyTag", "onRestoreInstanceState");
textviewshow.setText(savedInstanceState.getString(TEXT_KEY));
}
}
当您恢复变量时,您需要将其设置到视图中。
此外,您不需要ettext
变量,为了使代码更清晰,您可以像这样做(我没有在代码中实现OnClickListenner并将其设置,我认为这是更好的做法):
public class MainActivity extends AppCompatActivity {
private final String TEXT_KEY = "TEXT_KEY";
Button buttonshow;
EditText edittextname;
TextView textviewshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "onCreate");
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textviewshow.setText(edittextname.getText().toString());
}
});
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("MyTag", "onSaveInstanceState");
outState.putString(TEXT_KEY, textviewshow.getText().toString());
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d("MyTag", "onRestoreInstanceState");
textviewshow.setText(savedInstanceState.getString(TEXT_KEY));
}
}
英文:
Do it like this:
public class MainActivity extends AppCompatActivity
{
private final String TEXT_KEY = "TEXT_KEY";
Button buttonshow;
EditText edittextname;
TextView textviewshow;
String etText;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "onCreate");
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
etText = edittextname.getText().toString();
textviewshow.setText(etText);
}
});
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
Log.d("MyTag", "onSaveInstanceState");
outState.putString(TEXT_KEY, etText);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
Log.d("MyTag", "onRestoreInstanceState");
etText = savedInstanceState.getString(TEXT_KEY);
textviewshow.setText(etText);
}
}
When You restore the variable You have to set it to the View.
<hr>
You also don't need ettext
variable so to make it more clear You can do it like this (I don't implement OnClickListenner and set it in code, I think it is a better way to do it):
public class MainActivity extends AppCompatActivity
{
private final String TEXT_KEY = "TEXT_KEY";
Button buttonshow;
EditText edittextname;
TextView textviewshow;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "onCreate");
buttonshow = findViewById(R.id.buttonShow);
edittextname = findViewById(R.id.editTextName);
textviewshow = findViewById(R.id.textViewShow);
buttonshow.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
textviewshow.setText(edittextname.getText().toString());
}
});
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState)
{
super.onSaveInstanceState(outState);
Log.d("MyTag", "onSaveInstanceState");
outState.putString(TEXT_KEY, textviewshow.getText().toString());
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
Log.d("MyTag", "onRestoreInstanceState");
textviewshow.setText(savedInstanceState.getString(TEXT_KEY));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论