0

我有一个登录按钮,按下该按钮会将用户发送到另一个活动,并将在 TextView 中发布用户名。那部分工作正常。问题是当我尝试将第二个活动 TextView 中的用户名传递给第三个活动 TextView 时,它没有显示出来。android:hint 显示为文本,所以我知道 xml 很好。我已经尝试了在这个网站上可以找到的每一种“putExtra”意图。我想我只是错过了一些东西..帮助。我对 android 完全陌生,所以请嘲笑新手 :)

活动二

  public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
Button scanButton;
Button userinfo;
Button help;
Button receipt;
TextView showmsg;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /**
     * Dashboard Screen for the application
     * */        
    // Check login status in database
    userFunctions = new UserFunctions();
    if(userFunctions.isUserLoggedIn(getApplicationContext())){
        setContentView(R.layout.dashboard);



        scanButton = (Button) findViewById(R.id.scanButton);
        btnLogout = (Button) findViewById(R.id.btnLogout);
        userinfo =  (Button) findViewById(R.id.userinfo);
        help =  (Button) findViewById(R.id.help);
        receipt = (Button)findViewById(R.id.receipt);
        showmsg = (TextView)findViewById(R.id.showmsg);

        Intent in = getIntent();
        if (in.getCharSequenceExtra("usr") != null) {
            final TextView setmsg = (TextView)findViewById(R.id.showmsg);
            setmsg.setText("User: "+in.getCharSequenceExtra("usr"));                
        }




        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
            }
        });


        // Link to CC
        scanButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        MyScanActivity.class);
                startActivity(i);
                finish();
            }
        });

        // Create a customer receipt
        receipt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent intent = new Intent(DashboardActivity.this, ReceiptActivity.class));
                intent.putExtra("TextView",showmsg);
                startActivity(intent);
                finish();
            }
        });

        // Link to User info
        userinfo.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        UserInfoActivity.class);
                startActivity(i);
                finish();
            }
        });

        // Link to Help info
        help.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        HelpActivity.class);
                startActivity(i);
                finish();
            }
        });



    }else{
        // user is not logged in show login screen
        Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(login);
        // Closing dashboard screen
        finish();
    }




}

}

4

2 回答 2

1

@Smulian朱利安:

有一个错误。您正在传递组件TextView传递给下一个意图,而您应该只传递它的文本。

您目前正在这样做:

intent.putExtra("TextView",showmsg);

但你应该这样做

Bundle extras = myIntent.getExtras();
 extras .putString("TextView",showmsg.getText().toString());

在第三个活动中你应该得到这个值:

bundle = getIntent().getExtras();
bundle.getString("TextView");

然后将此值设置到您的文本视图中。

TextView yourTV = ((TextView) findViewById(R.id.your_textview_id));
yourTV.setText(bundle.getString("TextView"));

希望能帮助到你。

于 2013-04-18T15:12:08.580 回答
1

登录后,尝试将用户名保存在其中SharedPreferences,然后您可以随时随地获取它的价值:

保存用户名:

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
prefs.edit().putString("username", username).commit();

从 yourActivity 检索用户名的值:

SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
String username = prefs.getString("username");
yourTextView.setText(username);
于 2013-04-18T15:01:35.777 回答