如何使用Sharedpreferences

时间:2014-06-26 08:11:20

标签: android sharedpreferences

我想知道如何使用共享Prefrences .... 我的问题是我正在使用共享Prefrence ...我没有填写编辑文本 我的If条件不起作用...当我在编辑文本中没有填充时按下..Curser转到下一个活动 我的条件不起作用 我的调试器显示没有价值不是空的..如果我填写信息degugger向我显示我输入的值

这是我的代码

public class SignupE extends Activity {
SharedPreferences pref;
Context context;
Editor editor;
Button nexttoFinish;
EditText fulname,username,Datofbirth,Email,Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signupe);
    pref=getApplicationContext().getSharedPreferences("Mydata",
            Context.MODE_PRIVATE);

     nexttoFinish=(Button)findViewById(R.id.nexttofinish);
     fulname=(EditText)findViewById(R.id.fulname);
     username=(EditText)findViewById(R.id.user_name);
     Datofbirth=(EditText)findViewById(R.id.dateofbrth);
     Email=(EditText)findViewById(R.id.email_user);
     Password=(EditText)findViewById(R.id.pwd_user);
    nexttoFinish=(Button)findViewById(R.id.nexttofinish);




    nexttoFinish.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            String Fullname=fulname.getText().toString();
            String Username=username.getText().toString();
            String DateofBirth=Datofbirth.getText().toString();
            String E_Mail=Email.getText().toString();
            String PassWord=Password.getText().toString();
            if(fulname.equals(""))
            {
                Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
                return;
            }
            if(username.equals(""))
            {
                Toast.makeText(SignupE.this,"Please Enter Your User Name To Display", Toast.LENGTH_SHORT).show();
                return;
            }if(Datofbirth.equals(""))
            {
                Toast.makeText(SignupE.this,"Please Enter Your Date Of Birth", Toast.LENGTH_SHORT).show();
                return;
            }

            if(Email.equals(""))
            {
                Toast.makeText(SignupE.this,"Please Enter Email", Toast.LENGTH_SHORT).show();
                return;
            }

             if(Password.equals(""))
            {               
                Toast.makeText(SignupE.this,"Please Enter Strong password", Toast.LENGTH_SHORT).show();
                return;
            }

            editor=pref.edit();
            editor.putString("FullName", Fullname);
            editor.putString("UserName", Username);
            editor.putString("DatOfBirth", DateofBirth);
            editor.putString("Email", E_Mail);
            editor.putString("PassWord", PassWord);
            editor.commit();
            Intent F=new Intent(getApplicationContext(),SignUp.class);
            startActivity(F);


        }
    });
}

  }

6 个答案:

答案 0 :(得分:1)

使用此

 if(Fullname.matches(""))
            {
                Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
                return;
            }

答案 1 :(得分:1)

试试这个..

您的等同于名称EditText,而不是全名String

        String Fullname=fulname.getText().toString();
        String Username=username.getText().toString();
        String DateofBirth=Datofbirth.getText().toString();
        String E_Mail=Email.getText().toString();
        String PassWord=Password.getText().toString();
        if(Fullname.equals(""))
        {
            Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
            return;
        }else if(Username.equals(""))
        {
            Toast.makeText(SignupE.this,"Please Enter Your User Name To Display", Toast.LENGTH_SHORT).show();
            return;
        }else if(DateofBirth.equals(""))
        {
            Toast.makeText(SignupE.this,"Please Enter Your Date Of Birth", Toast.LENGTH_SHORT).show();
            return;
        }else if(E_Mail.equals(""))
        {
            Toast.makeText(SignupE.this,"Please Enter Email", Toast.LENGTH_SHORT).show();
            return;
        }else if(PassWord.equals(""))
        {               
            Toast.makeText(SignupE.this,"Please Enter Strong password", Toast.LENGTH_SHORT).show();
            return;
        }else{

            editor=pref.edit();
        editor.putString("FullName", Fullname);
        editor.putString("UserName", Username);
        editor.putString("DatOfBirth", DateofBirth);
        editor.putString("Email", E_Mail);
        editor.putString("PassWord", PassWord);
        editor.commit();
        Intent F=new Intent(getApplicationContext(),SignUp.class);
        startActivity(F);
        }

答案 2 :(得分:0)

像这样更改你的代码

 .................................

if(Fullname.equals(""))
            {               
                Toast.makeText(SignupE.this,"Please Enter Strong password", Toast.LENGTH_SHORT).show();
                return;
            }
else{
            editor=pref.edit();
            editor.putString("FullName", Fullname);
            editor.putString("UserName", Username);
            editor.putString("DatOfBirth", DateofBirth);
            editor.putString("Email", E_Mail);
            editor.putString("PassWord", PassWord);
            editor.commit();
            Intent F=new Intent(getApplicationContext(),SignUp.class);
            startActivity(F);
}

答案 3 :(得分:0)

在这段代码中你有很多变量混淆,例如在你所有的if条件中你在edittext上使用equals()方法,这将永远不会是真的,因此不会显示任何toast。

将所有if条件更改为以下内容,它应该可以正常工作。

String Fullname=fulname.getText().toString();
        String Username=username.getText().toString();
        String DateofBirth=Datofbirth.getText().toString();
        String E_Mail=Email.getText().toString();
        String PassWord=Password.getText().toString();
        if(fulname.equals(""))
        {
            Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
            return;
        }
        //In all your If condition you either need to check the values from your Strings
        //or you can directly use getText().toString().equals("") method on each of your edittext
        if(Username.equals(""))  //change in this line
        {
            Toast.makeText(SignupE.this,"Please Enter Your User Name To Display", Toast.LENGTH_SHORT).show();
            return;
        } else if(DatofBirth.equals("")) //change in this line
        {
            Toast.makeText(SignupE.this,"Please Enter Your Date Of Birth", Toast.LENGTH_SHORT).show();
            return;
        }

        else if(E_mail.equals(""))   //change in this line
        {
            Toast.makeText(SignupE.this,"Please Enter Email", Toast.LENGTH_SHORT).show();
            return;
        }

         else if(PassWord.equals(""))   //change in this line
        {               
            Toast.makeText(SignupE.this,"Please Enter Strong password", Toast.LENGTH_SHORT).show();
            return;
        }

这应该解决你的toast没有显示问题但是这段代码仍将继续执行并转到下一个活动,所有sharedpreferences空字符串及其键。如果要在填充所有edittext并添加新意图后将此值添加到sharedpreference,则应该将最后一个块放入其中并将代码移动到其中,如下所示,

        //This block will only be executed if all edittexts are filled with something
        else {   //put the else block after your last if condition
        editor=pref.edit();
        editor.putString("FullName", Fullname);
        editor.putString("UserName", Username);
        editor.putString("DatOfBirth", DateofBirth);
        editor.putString("Email", E_Mail);
        editor.putString("PassWord", PassWord);
        editor.commit();
        Intent F=new Intent(getApplicationContext(),SignUp.class);
        startActivity(F); 
        }

答案 4 :(得分:0)

我想你试图进行验证?如果用户填写所有textview,而不是按下按钮,您可以禁用该按钮并启用它。试试这个:

private class CheckTextifEmpty implements TextWatcher {
    @Override
    public void afterTextChanged(Editable s) {
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if (isEmptyCharacters(serverNumber)
                || isEmptyCharacters(textview1)
                || isEmptyCharacters(textview2)
                || isEmptyCharacters(textview3)) {
            //do something here
                           nexttoFinish.setEnabled(false);
        } else {
            //do something here
                           nexttoFinish.setEnabled(true);
        }
    }
}



   private boolean isEmptyCharacters(TextView view){
       return view.getText().toString().trim().isEmpty();
}

将它添加到您的所有textview中:

fulname.addTextChangedListener(new CheckTextifEmpty());
username.addTextChangedListener(new CheckTextifEmpty());

答案 5 :(得分:0)

首先看一下共享偏好here是什么,远离你的问题,我只是明白,当你给出一个空值并且你的警报不起作用时,你并不满意。

尝试替换

if(fulname.equals(""))
            {
                Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
                return;
            }

if(fulname.contains(""))
            {
                Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
                return;
            }

 if (TextUtils.isEmpty(fulname)) {
        Toast.makeText(SignupE.this,"Please Your Full Name", Toast.LENGTH_SHORT).show();
                    cancel = true;
                }