在我的应用程序中,用户在注册活动中输入其用户名,然后单击“提交”按钮,则该用户名应显示在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中的TextView
<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"/>
最佳答案
假设您具有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);