【问题标题】:How to add code for editText2 box?如何为editText2框添加代码?
【发布时间】:2011-06-13 01:31:04
【问题描述】:

请看我的代码。如您所见,它有一个 editText 以及它的作用——它将文本保存在 editText 中。我在网上找到了这段代码。它与我的布局完美配合。但是我添加了另一个 editText 框,我称之为 editText2 并且无法弄清楚如何对其进行编码。如何使第二个中的文本也被保存?我需要在 src 中创建一个新类吗?我知道我必须添加 editText2 和 editBox2 ,但是如何以及在哪里?有人可以只给我一个示例,以便我可以将它用于整个代码吗?例如,在受保护的 void onCreate-我如何添加 editText2?谢谢!

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;

}

【问题讨论】:

标签: android add editbox


【解决方案1】:

您需要学习一些有关布局和 UI 小部件的基础知识。 editBox1 定义在main.xml 下的res\layout 目录中。打开该文件并查看editBox1,复制代码并将id 更改为editBox2。然后在onCreate()中添加代码editBox2 =(EditText)findViewById(R.id.editText2); 获取参考

【讨论】:

    【解决方案2】:

    我正在回答我自己的问题。它可以 100% 地在两个编辑文本框上保存状态。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2013-08-20
      • 2015-04-08
      相关资源
      最近更新 更多