Android:登录后如何获取用户名信息,并在MainActivity中显示welcome'username'

时间:2011-06-02 17:29:38

标签: android login textview username sharedpreferences

我已经做了很多搜索,其中大部分都没有用于安卓。

我使用sharedpref在会话中保存用户名,直到退出。我想在mainactivity中显示欢迎'用户名'。

。现在我想要一个示例代码来获取在sharedacfs中保存的mainactivity类中的'username'并在textview中显示它。

下面是我打开mainActivity的登录类

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("username", username);
        editor.commit();

        if(prefs.getString("username", null)!=null)
        {Intent i = new Intent(getApplicationContext(), Customer.class); 
        startActivity(i);}

        etUsername = (EditText)findViewById(R.id.username);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogin.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View v) {
            // Check Login
            String username = etUsername.getText().toString();


            if(username.equals("1111")){
                lblResult.setText("Login successful.");

               Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                            } 
            else if(username.equals("2222")){
                lblResult.setText("Login successful.");

                Intent i = new Intent(getApplicationContext(), MainActivity2.class);
                startActivity(i);

            }
btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Close the application
            finish();

                }
            });   }

MainActivity.java

public class MainActivity extends ListActivity
{

    TextView selection;
    CustomerListItem[] items = { 
            new CustomerListItem("Start Trip", StartTripActivity.class), 
            new CustomerListItem("Clock in", ClockinActivity.class), 
            new CustomerListItem("Log Out", LogoutActivity.class)};
    private TextView resultsTxt;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.customer);
        setListAdapter(new ArrayAdapter<CustomerListItem>(
                this, android.R.layout.simple_list_item_1, items));
        selection = (TextView) findViewById(R.id.selection);
showname = (TextView) findViewById(R.id.showname);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);
        final Intent intent = new Intent(this, items[position].getActivity());
        startActivityForResult(intent, position);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK)
        {
            // Perform different actions based on from which activity is
            // the application returning:
            switch (requestCode)
            {
                case 0:
                    // TODO: handle the return of the 
                    break;
                case 1:
                    // TODO: handle the return of the                     
break;
                case 2:
                    // TODO: handle the return of the                    
break;
                default:
                    break;
            }
        }
        else if (resultCode == RESULT_CANCELED)
        {
            resultsTxt.setText("Canceled");
        }
    }
}

2 个答案:

答案 0 :(得分:4)

...试

在您的登录活动中:

SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putString("username", username).commit();
Intent i = new Intent(this, MainActivity.class); 
startActivity(i);

在您的主要活动中......

public class MainActivity extends Activity {

    private String username = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(...) here

        SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
        username = prefs.getString("username", "UNKNOWN");

        ...

    }
}

答案 1 :(得分:2)

首先,您应该将用户名作为额外内容传递,以便下一个活动可以抓取它。把它放在你的登录活动中:

String username = prefs.getString("username");

Intent i = new Intent(this, MainActivity.class);
// this is where you should pass the username
i.putExtra("username", username);
startActivity(i);

之后,将其放在MainActivity中,可能是onCreate方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainview);

    Bundle extras = getIntent().getExtras();

    if (extras.containsKey("username")) {
        String username = extras.getString("username");

        // put whatever code you want here to show the username

    }
}

希望它能回答你的问题。