我试图通过"Start Another Activity"教程,但是,经过整个事情(两次),应用程序仍然拒绝返回在文本框中输入的文本 - 它总是返回&#34 ; Hello World"。
我试图弄明白,并怀疑它可能与fragment_display_message.xml
有关,其中EditText
调用Hello World字符串。但我真的不知道。
任何帮助将不胜感激。这是代码:
MainActivity.java
package com.knargle.streetiteatit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.knargle.streetiteatit.MESSAGE";
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.knargle.streetiteatit.MainActivity$PlaceholderFragment" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java
package com.knargle.streetiteatit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// 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);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
fragment_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.knargle.streetiteatit.DisplayMessageActivity$PlaceholderFragment" >
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hello_world" />
</RelativeLayout>
答案 0 :(得分:0)
setContentView
中DisplayMessageActivity
被调用两次。删除第二个:
setContentView(R.layout.activity_display_message);
答案 1 :(得分:0)
在DisplayMessageActivity
:
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_display_message);
你拨打setContentView
两次,这只会覆盖已经存在的内容。删除第二个setContentView
。
答案 2 :(得分:0)
从DisplayMessageActivity
:
// 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);
您试图在新的文本视图中设置消息,该视图不是布局文件的一部分。然后将该文本视图设置为布局的ContentView。这似乎是一种令人费解的方法。而是尝试以下方法:
在setContentView(R.layout.activity_display_message);
行之后,添加以下行:
EditText editText = (EditText) findViewById(R.id.edit_message);
editText.setText(message);
这样,您可以使用布局文件中的现有Edit文本元素来显示消息。您的原始代码从未在此元素中替换HelloWorld。这就是为什么它总是向你展示HelloWorld而不是你的消息。
答案 3 :(得分:0)
在DisplayMessageActivity.java
文件中,您正在调用setContentView
两次,因为它会覆盖第一行代码。目前您的代码如下所示:
setContentView(textView);
setContentView(R.layout.activity_display_message); // Delete this line. As this line of code appears in your java file, it is overwriting the 'text' you entered with the default 'hello world' message
应该是这样的:
setContentView(textView);
答案 4 :(得分:0)
您可以按照Andrew和Rajesh的回答。但事实上你已经定义了一个布局fragment_display_message,我相信你想要使用它。在这种情况下,您需要做的就是摆脱代码以动态添加TextView和相应的setContentView,并保留第二个使用您的布局的setContentView。完成后,您将获得对EditText的引用,并将其文本设置为Intent中发送的内容。