对于 Android 开发非常陌生。我有一个培训应用程序,有一个文本输入字段和一个按钮。如果输入的文本与某个单词匹配,则单击按钮
会创建一个新页面( Intent ?),其中显示一些文本。
我遇到的问题是,通过单击按钮创建的新页面没有与主页相同的背景图像。我不知道为什么。这是迄今为止我拥有的相关代码。如果需要,我可以发布更多内容。
**fragment_welcome_screen.xml**
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/bg_pic">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
这部分工作正常并显示 bg_pic。
WelcomeScreen.java
public void sendMessage(View view) {
// Do something in response to button
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
if ((message.equals("Secret"))||(message.equals("secret"))) {
Intent intent = new Intent(this,
DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
我希望该按钮仅在输入“Secret”时才起作用。
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
//String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
String message = "Test String";
TextView textView = new TextView(this);
textView.setTextSize(24);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
我知道我可能在这里做错了什么。
activity_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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/test_pic"
tools:context="com.example.varun.myapplication.DisplayMessageActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="@drawable/test_pic"/>
</RelativeLayout>
我希望 test_pic 是按下按钮时显示的页面的背景。但事实并非如此。背景只是白色。
我意识到这可能是一个微不足道的问题,但有人可以帮我吗?我真的很感激。
谢谢。
最佳答案
为什么在 DisplayMessageActivity.java 中使用 setContentView() 两次。删除第二个 setcontentview() 并为 Activity_display_message.xml 中的 TextView 分配一些 id。这是为textview分配id的代码。
<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:background="@drawable/test_pic"
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.example.varun.myapplication.DisplayMessageActivity">
<TextView
android:id="@+id/mytv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/test_pic"
android:text="@string/hello_world"/>
并获取带有该 id 引用的 TextView ,例如
TextView textView = (TextView)findViewById(R.id.mytv);
并使用此 TextView 引用来显示来自上一个 Activity 的消息,如下所示
textView.setText(message);
希望这对您有帮助。
关于java - Android 点击按钮后背景图像消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26728236/