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

huangapple go评论74阅读模式
英文:

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 = &quot;TEXT_KEY&quot;;
    Button buttonshow;
    EditText edittextname;
    TextView textviewshow;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(&quot;MyTag&quot;, &quot;onCreate&quot;);

        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(&quot;MyTag&quot;, &quot;onSaveInstanceState&quot;);
        outState.putString(TEXT_KEY, textviewshow.getText().toString());
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(&quot;MyTag&quot;, &quot;onRestoreInstanceState&quot;);
        textviewshow.setText(savedInstanceState.getString(TEXT_KEY));
    }
}

huangapple
  • 本文由 发表于 2020年9月24日 00:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64032289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定