通过活动传递的参数不会显示为TextView

时间:2015-09-28 23:27:19

标签: android textview

在我的应用中,用户在注册活动中输入用户名,当他点击提交按钮时,他的用户名应显示在newprofile活动的TextView中。我的问题是没有显示TextView。

我的NewProfile.class代码:

package dmst.allamoda;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
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.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class NewProfile extends ActionBarActivity {

public static String PUBLIC_STATIC_STRING_IDENTIFIER = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_newprofile);
    Button btnSimple1 = (Button) findViewById(R.id.home);
    btnSimple1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent1 = new Intent(v.getContext(), Home3.class);
            startActivity(intent1);
        }
    });
    Button btnSimple2 = (Button) findViewById(R.id.profile);
    btnSimple2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent2 = new Intent(v.getContext(), NewProfile.class);
            startActivity(intent2);
        }
    });

    Button btnSimple3 = (Button) findViewById(R.id.friends);
    btnSimple3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent3 = new Intent(v.getContext(), Friends2.class);
            startActivity(intent3);
        }
    });

    Button btnSimple4 = (Button) findViewById(R.id.logout);
    btnSimple4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent4 = new Intent(v.getContext(), MainActivity.class);
            startActivity(intent4);
        }
    });
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (1) : {
            if (resultCode == NewProfile.RESULT_OK) {
                String newText = data.getStringExtra(PUBLIC_STATIC_STRING_IDENTIFIER);
                TextView textView = (TextView) findViewById(R.id.username);
                textView.setTextSize(40);
                textView.setText(newText);
                setContentView(textView);
            }
            break;
        }
    }
}
}

和我的activity_newprofile.xml中的T​​extView

                    <TextView
                    android:id="@+id/username"
                    android:inputType="text"
                    android:layout_width="250dp"
                    android:layout_height="50dp"
                    android:textColorHint="#000000"
                    android:layout_marginTop="20dp"
                    android:layout_marginLeft="15dp"
                    android:textColor="#000000"
                    android:gravity="top"/>

1 个答案:

答案 0 :(得分:1)

假设您有来自SignUpActivity.java的用户名​​,您需要使用Intent Extra将该信息传递给NewProfile活动以显示它。这是

的方式

在您的SignupActivity中,调用NewProfile活动时:

Intent intent = new Intent(SignUpActivity.this, NewProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("username", the username that user entered);
startActivity(intent);

然后在您的NewProfileActivity中,您可以获取/设置用户名

// get the username
final String userName = getIntent().getStringExtra("username");

// set the username
yourTextView.setText(username);