我在活动类中使用sharedprefarance保存了一些数据,现在我可以访问主活动类中的保存数据

时间:2015-04-28 19:05:53

标签: android

我想设置密码 首先,我将数据保存在共享前言中, 然后我想从mainActivity访问保存的数据, 我在哪里设置登录系统。

    package com.example.akibmahmud.pasword;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class SetPassword extends ActionBarActivity {
        final static String SHARED_NAME="akib";
        final static String PASSWORD_NAME="password";
        EditText cPass,newPass;
        Button setButton;
        SharedPreferences sharedPreferences;

        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_set_password);
            cPass= (EditText) findViewById(R.id.password1);
            newPass= (EditText) findViewById(R.id.password2);
            setButton= (Button) findViewById(R.id.setbutton);



          sharedPreferences=getSharedPreferences(SHARED_NAME,MODE_PRIVATE);
            final String `passwod_name=sharedPreferences.getString(PASSWORD_NAME,"1234");`

            setButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String oPass=cPass.getText().toString();
                    String nPass=newPass.getText().toString();
                    if (passwod_name.equals(oPass)){
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString(PASSWORD_NAME,nPass);
                        editor.commit();

                    }
                    else{
                        Toast.makeText(getApplicationContext(),"Please Enter Your Right Current Password",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }



    }

我需要,我想要从Main_Activity类访问(passwod_name)

    package com.example.akibmahmud.pasword;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import java.util.Set;

    import static android.content.SharedPreferences.*;


    public class MainActivity extends ActionBarActivity {
        EditText pass,pass1;
        Button btn,btn1;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pass= (EditText) findViewById(R.id.pasword);
            btn= (Button) findViewById(R.id.nextSet);
            btn1= (Button) findViewById(R.id.logIn);
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String input = pass1.getText().toString();
                    if (?????.equals(input)) {
                        Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                        startActivity(intent);

                    } else {
                        Toast.makeText(getApplicationContext(), "Password Wrong,Please Enter Right Password", Toast.LENGTH_LONG).show();
                    }
                }
            });
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(MainActivity.this,SetPassword.class);
                    startActivity(intent);
                }
            });

        }        
    }

3 个答案:

答案 0 :(得分:0)

一个简单的谷歌查询(sharedpreferences所有活动)返回:

Storing data in SharedPreferences accessible to all activities

我觉得我找到了你的解决方案。但我不想复制,所以阅读它。如果仍然没有回答,请再次告诉我们。

答案 1 :(得分:0)

如果在访问SharedPreferences时使用与第一个参数相同的名称,这应该可以在MainActivity中使用:

final static String SHARED_NAME="akib";
final static String PASSWORD_NAME="password";    
SharedPreferences sharedPreferences;
sharedPreferences=getSharedPreferences(SHARED_NAME,MODE_PRIVATE);
String password  = sharedPreferences.getString(PASSWORD_NAME, "");

答案 2 :(得分:0)

SetPassword 类中,像这样使用SharedPreference:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PASSWORD_NAME,nPass);
        editor.commit();

在您 MainActivity 中访问数据使用SharedPreference,如下所示:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String password_data = sharedPreferences.getString("password", "");