gpt4 book ai didi

java - 如何在 Android Studio 中从 Onclick() 获取字符串值到 onSaveInstanceState()

转载 作者:行者123 更新时间:2023-12-04 15:17:33 25 4
gpt4 key购买 nike

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() .所以当我的 Activity 变成风景时,我可以恢复字符串。但我无法从 Onclick() 获得值(value)到其他方法。我需要 etText 的字符串值至 onSaveInstanceState()onRestoreInstanceState() .否则,这些方法不起作用。当 Activity 变为横向时,我无法恢复此字符串值。

最佳答案

像这样做:

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);
}
}
当您恢复变量时,您必须将其设置为 View 。

你也不需要 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));
}
}

关于java - 如何在 Android Studio 中从 Onclick() 获取字符串值到 onSaveInstanceState(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64032289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com