需要保存 EditText 框的状态。请帮忙!

发布于 2024-11-15 14:45:07 字数 1519 浏览 1 评论 0原文

我有一个 EditText 的代码,但我不知道如何为位于同一页面上的第二个 editText 框制作相同的代码。这是我的代码:

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 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拍不死你 2024-11-22 14:45:07

我正在回答我自己的问题。它可以 100% 保存两个 editText 框的状态。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文