14 июн. 2017 г.

Сохранить значение формы при смене ориентации экрана

package com.example.jaroslavh.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


EditText editText;
TextView textView;
TextWatcher tw;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText = (EditText) findViewById(R.id.editText2);
textView = (TextView) findViewById(R.id.textView);

tw = new TextWatcher() {

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

public void afterTextChanged(Editable s) {
editText.removeTextChangedListener(tw);
textView.setText(s.toString());
s.clear();
editText.addTextChangedListener(tw);
}

};

editText.addTextChangedListener(tw);
}

protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
textView.setText(savedInstanceState.getString("textView"));
}

protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("textView", textView.getText().toString());
}

}

Комментариев нет:

Отправить комментарий

 

CopyPaste Copyright © 2017 - |- Шаблон создан O Pregador - |- Отредактирован myr4ik07