0

I am new here, and new to Android Programming as well. So any help on this will be greatly appreciated.

I am running an Android App(basically the example given in developer.android.com) and customizing it, to add a navigation bar at the top which displays the screen number. The idea of the app is to let the user enter a message in the 1st screen, and display the message in the second screen. I've been able to setup a navigation bar in the 1st screen which displays(1/2), and have similarly defined a layout in the second screen's xml file which should display the navigation bar and (2/2) in it. Also, I have added a "Stop" button, which I will make use of later.

However, the problem I am facing is- the second screen displays only the message received from the user, and not the layout I want(navigation bar). I think I have correctly determined the root cause of this, to be the below code:

//Create the text view 
TextView textView = new TextView(this); 
textView.setTextSize(40); 
textView.setText(message); 

// Set the text view as the activity layout
setContentView(textView);

I think the setContentView() method that I've used here is displaying the message I received from the 1st activity. When I comment out this line, I get my desired formatting of the second screen by using:

setContentView(R.layout.activity_second_screen);

However, I am not able to understand fully why and how to club both the things together. It looks like the setContentView(R.layout..) doesn't work as expected when the setContentView(textView) is not commented, which is I guess, normal.

Thank You in advance.

Edit- Thank You, CodeMagic for such a prompt response. Before I try out your solution, here's the complete on create. I have some code in there which deals with a "stop" button, but am yet to implement it. So please ignore that.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);//Change-11/10 6:00 PM


Button buttonStop=(Button)findByViewId(R.id.buttonStop);//Change-11/10 6:00 PM
buttonStop.setOnClickListener(stopListener);//Change-11/10 6:00 PM

Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_second_screen);

// Create the text view - this code prints the message typed in first
//screen and displays on second screen, ignoring the xml layout of second screen     
TextView textView = new TextView(this); 
textView.setTextSize(40); 
textView.setText(message); 

// Set the text view as the activity layout
setContentView(textView); //change-11/11-9:00 PM
//setContentView(R.layout.activity_second_screen); 
}
4

2 回答 2

1

我认为您想要的只是在运行前添加TextView到您的activity_second_screen.xml。然后在你的中onCreate()你可以添加message似乎你得到正确的,即使它没有显示。所以做类似的事情

EditText et;

public void onCreate(Bundle bundle)
{
     super.onCreate(bundle);
     setContentView(R.layout.activity_second_screen);
     et = (EditText) findViewById(R.id.editTextID); // where editTextId is 
                                                    //the id you give the 
                                                   //EditText in your layout file
     et.setText(message); //this will be done after you get your message variable
                          // I assume through an Intent
}

那么你不需要

TextView textView = new TextView(this); 
textView.setTextSize(40); 

当您setContentView()每次调用不止一次时,它将设置View为任何内容setContentView()并覆盖之前调用的内容setContentView()

于 2013-11-12T03:14:50.057 回答
0

您可以通过使用从第一个屏幕intent.putExtra(key,value)发送消息并在第二个屏幕中使用此消息获取此消息,intent.getExtra(key)这将返回您在第一个活动中发送的值,并将获取此值并在第二个屏幕中显示您想要的任何位置

在第一个活动中,下一个按钮使用下面的代码

意图 i = new Intent(First.this, Second.class); i.putExtra("key", "value"); 开始活动(一);

在第二个活动 onCreate 方法中写下面的代码片段

Bundle extras = getIntent().getExtras();
String ans = extras.getString("key");
于 2013-11-12T07:06:01.653 回答