需要保存状态的EditText框。请帮忙! [英] Need Save state for EditText boxes. Please help!

查看:76
本文介绍了需要保存状态的EditText框。请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code一的EditText,但我无法弄清楚如何使同一code的第二EDITTEXT盒,位于同一页上。这是我的code:

 包tryone.now.forfreenow;
进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
进口android.widget.EditText;
进口android.widget.TextView;
公共类记事本扩展活动
{
   @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        编辑框=(EditText上)findViewById(R.id.editText1);
    }
    保护无效onResume(){
        super.onResume();
        共享preferences preFS = GET preferences(0);
        字符串restoredText = prefs.getString(文字,NULL);
        如果(restoredText!= NULL){
            editBox.setText(restoredText,TextView.BufferType.EDITABLE);
            INT selectionStart = prefs.getInt(选择开始,-1);
            INT选定结束= prefs.getInt(选择高端,-1);
            如果(selectionStart = -1&安培;!&安培;!选定结束= -1){
                editBox.setSelection(selectionStart,选定结束);
            }
        }
    }
    保护无效的onPause(){
        super.onPause();
        共享preferences.Editor编辑= GET preferences(0).edit();
        editor.putString(文本,editBox.getText()的toString());
        editor.putInt(选择开始,editBox.getSelectionStart());
        editor.putInt(选择高端,editBox.getSelectionEnd());
        editor.commit();
    }
    私人的EditText编辑框;
}
 

解决方案

我回答我自己question.It工程100%,以节省两个EDITTEXT箱状态。

 包tryone.now.forfreenow;

进口android.app.Activity;
进口android.content.Shared preferences;
进口android.os.Bundle;
进口android.widget.EditText;
进口android.widget.TextView;

公共类记事本扩展活动
{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        编辑框=(EditText上)findViewById(R.id.editText1);
        editBox2 =(EditText上)findViewById(R.id.editText2);
    }

    保护无效onResume(){
        super.onResume();

        共享preferences preFS = GET preferences(0);
        字符串restoredText = prefs.getString(文字,NULL);
        如果(restoredText!= NULL)
        {
            editBox.setText(restoredText,TextView.BufferType.EDITABLE);

            INT selectionStart = prefs.getInt(选择开始,-1);
            INT选定结束= prefs.getInt(选择高端,-1);
            如果(selectionStart = -1放大器;!&安培;!选定结束= -1)
            {
                editBox.setSelection(selectionStart,选定结束);
            }
            共享preferences preFS2 = GET preferences(1);
            字符串restoredText2 = prefs2.getString(文本2,NULL);
            如果(restoredText2!= NULL)
            {
                editBox2.setText(restoredText2,TextView.BufferType.EDITABLE);

                INT selectionStart2 = prefs2.getInt(选择 -  START2,-1);
                INT selectionEnd2 = prefs2.getInt(选择 -  END2,-1);
                如果(!selectionStart2 = -1&功放;&安培; selectionEnd2!= -1)
                {
                    editBox2.setSelection(selectionStart2,selectionEnd2);
                }
            }
        }
    }

    保护无效的onPause(){
        super.onPause();

        共享preferences.Editor编辑= GET preferences(0).edit();
        editor.putString(文本,editBox.getText()的toString());
        editor.putInt(选择开始,editBox.getSelectionStart());
        editor.putInt(选择高端,editBox.getSelectionEnd());
        editor.commit();

        共享preferences.Editor editor2 = GET preferences(1).edit();
        editor2.putString(文本2,editBox2.getText()的toString());
        editor2.putInt(选择 -  START2,editBox2.getSelectionStart());
        editor2.putInt(选择 -  END2,editBox2.getSelectionEnd());
        editor2.commit();
    }

    私人的EditText编辑框;
    私人的EditText editBox2;
}
 

I have code for one EditText, but I can not figure out how to make same code for second editText Box, that is located on the same page. Here is my code:

package tryone.now.forfreenow;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class notepad extends Activity
{
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editBox =(EditText)findViewById(R.id.editText1);
    }
    protected void onResume() {
        super.onResume();
        SharedPreferences prefs = getPreferences(0); 
        String restoredText = prefs.getString("text", null);
        if (restoredText != null) {
            editBox.setText(restoredText, TextView.BufferType.EDITABLE);
            int selectionStart = prefs.getInt("selection-start", -1);
            int selectionEnd = prefs.getInt("selection-end", -1);
            if (selectionStart != -1 && selectionEnd != -1) {
                editBox.setSelection(selectionStart, selectionEnd);
            }
        }
    }
    protected void onPause() {
        super.onPause();
        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putString("text", editBox.getText().toString());
        editor.putInt("selection-start", editBox.getSelectionStart());
        editor.putInt("selection-end", editBox.getSelectionEnd());
        editor.commit();
    }
    private EditText editBox;
}

解决方案

I am answering my own question.It works 100% to save state on two editText boxes.

package tryone.now.forfreenow;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class notepad extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editBox = (EditText) findViewById(R.id.editText1);
        editBox2 = (EditText) findViewById(R.id.editText2);
    }

    protected void onResume() {
        super.onResume();

        SharedPreferences prefs = getPreferences(0);
        String restoredText = prefs.getString("text", null);
        if (restoredText != null)
        {
            editBox.setText(restoredText, TextView.BufferType.EDITABLE);

            int selectionStart = prefs.getInt("selection-start", -1);
            int selectionEnd = prefs.getInt("selection-end", -1);
            if (selectionStart != -1 && selectionEnd != -1)
            {
                editBox.setSelection(selectionStart, selectionEnd);
            }
            SharedPreferences prefs2 = getPreferences(1);
            String restoredText2 = prefs2.getString("text2", null);
            if (restoredText2 != null)
            {
                editBox2.setText(restoredText2, TextView.BufferType.EDITABLE);

                int selectionStart2 = prefs2.getInt("selection-start2", -1);
                int selectionEnd2 = prefs2.getInt("selection-end2", -1);
                if (selectionStart2 != -1 && selectionEnd2 != -1)
                {
                    editBox2.setSelection(selectionStart2, selectionEnd2);
                }
            }
        }
    }

    protected void onPause() {
        super.onPause();

        SharedPreferences.Editor editor = getPreferences(0).edit();
        editor.putString("text", editBox.getText().toString());
        editor.putInt("selection-start", editBox.getSelectionStart());
        editor.putInt("selection-end", editBox.getSelectionEnd());
        editor.commit();

        SharedPreferences.Editor editor2 = getPreferences(1).edit();
        editor2.putString("text2", editBox2.getText().toString());
        editor2.putInt("selection-start2", editBox2.getSelectionStart());
        editor2.putInt("selection-end2", editBox2.getSelectionEnd());
        editor2.commit();
    }

    private EditText editBox;
    private EditText editBox2;
}

这篇关于需要保存状态的EditText框。请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆