首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >股份优惠问题

股份优惠问题
EN

Stack Overflow用户
提问于 2013-09-05 07:48:09
回答 3查看 227关注 0票数 3

我已经创建了一个页面,其中我接收字符串和int在EditText,然后存储在共享首选项点击保存按钮,但我不能使它的功能。具体来说,当我重新打开页面数据丢失时,不会存储数据。请帮帮忙

代码语言:javascript
复制
public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   tA = edtA.getText().toString();
   edtB = (EditText) findViewById(R.id.etB);
   tB = edtB.getInputType();
   edtC = (EditText) findViewById(R.id.etC);
   tC = edtC.getInputType();
   edtD = (EditText) findViewById(R.id.etD);
   tD = edtD.getInputType();
   edtE = (EditText) findViewById(R.id.etE);
   tE = edtE.getInputType();
   edtF = (EditText) findViewById(R.id.etF);
   tF = edtF.getInputType();

   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");

               abcPref = PreferenceManager.getDefaultSharedPreferences(Abc.this);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref= getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
} 
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-05 08:01:45

我已经更新了您的代码,它可能会帮助您:

代码语言:javascript
复制
 public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   tA = edtA.getText().toString();
   edtB = (EditText) findViewById(R.id.etB);
   tB = edtB..getText().toString();
   edtC = (EditText) findViewById(R.id.etC);
   tC = edtC..getText().toString();
   edtD = (EditText) findViewById(R.id.etD);
   tD = edtD..getText().toString();
   edtE = (EditText) findViewById(R.id.etE);
   tE = edtE..getText().toString();
   edtF = (EditText) findViewById(R.id.etF);
   tF = edtF..getText().toString();

// Initialize your sharedpreference here
abcPref = getApplicationContext().getSharedPreferences("filename", 0); // 0 - for private mode



   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");
// Here is opening sharedpreference which you have edited in save button
               abcPref = Abc.this.getSharedPreferences(filename,0);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref=  Abc.this.getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
}

//从SharedPreferences获取数据

代码语言:javascript
复制
 abcPref=  <<YourActivityName>>.this.getSharedPreferences(filename,0);

    String str1= abcPref.getString("field1Data", "DefaultValue_If_valueIsNull");
    String str2=abcPref.getString("field2Data", "DefaultValue_If_valueIsNull");
    Log.i("Log cat values checking","str1="+str1 +"  str2="+str2);
票数 1
EN

Stack Overflow用户

发布于 2013-09-05 07:54:13

  • 要么使用PreferenceManager.getDefaultSharedPreferences(Abc.this);保存和获取值,要么使用getSharedPreferences(filename,0);
  • 对于整数强制EditText只使用数字。使用android:inputType="number"

代码语言:javascript
复制
@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        abcPref= PreferenceManager.getDefaultSharedPreferences(Abc.this);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA.getText().toString());
        editor.putInt("field2Data", Integer.parseInt(tB.getText().toString())); 
        editor.putInt("field3Data", Integer.parseInt(tC.getText().toString()));
        editor.putInt("field4Data", Integer.parseInt(tD.getText().toString()));
        editor.putInt("field5Data", Integer.parseInt(tE.getText().toString()));
        editor.commit();
    }
票数 0
EN

Stack Overflow用户

发布于 2013-09-05 08:44:16

请您确认一下,您是否正确地填充了来自SharedPref的数据来加载data.If,给出一个简短的说明。我不能对此发表评论。

编辑:

代码语言:javascript
复制
public class Abc extends Activity{
Button one2five, save1;
EditText edtA, edtB, edtC, edtD, edtE, edtF;
String tA;
int tB, tC, tD, tE, tF;
public static String filename = "MySharedString";
SharedPreferences abcPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.abc);
   one2five = (Button) findViewById(R.id.btp1);
   save1 = (Button) findViewById(R.id.btps1);
   edtA = (EditText) findViewById(R.id.etA);
   edtB = (EditText) findViewById(R.id.etB);
   edtC = (EditText) findViewById(R.id.etC);
   // others..
 }
 public void onResume(){
  super.onResume();
   abcPref= getSharedPreferences(filename,0);
   edtA.setText(abdPref.getString("field1Data", null));
   // set others' like


   one2five.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
           // TODO Auto-generated method stub
           Intent openg2j = new Intent("com.sport.sport.G2J");

               abcPref = PreferenceManager.getDefaultSharedPreferences(Abc.this);
               abcPref.getInt("filename", 0);

               startActivity(openg2j);
       }
   });
   save1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        tA = edtA.getText().toString();
        tB = edtB.getInputType();
        tC = edtC.getInputType();
        //others..

        abcPref= getSharedPreferences(filename,0);
        SharedPreferences.Editor editor = abcPref.edit();
        editor.putString("field1Data", tA);
        editor.putInt("field2Data", tB); 
        editor.putInt("field3Data", tC);
        editor.putInt("field4Data", tD);
        editor.putInt("field5Data", tE);
        editor.commit();
    }
});
}
} 

看看这个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18630610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档