简体   繁体   English

单击按钮后,Android背景图像消失

[英]Android Background image disappears after button click

Very new to Android development. Android开发的新手。 I have a training app that has a text input field and a button. 我有一个具有文本输入字段和按钮的培训应用程序。 If the entered text matches a word, then the button click creates a new page (Intent?) which displays some text. 如果输入的文本与单词匹配,则button click会创建一个新页面(意图?),该页面显示一些文本。

The problem I'm having is that the new page that is created by the button click does not have the same background image as the main page. 我遇到的问题是,通过按钮单击创建的新页面没有与主页相同的背景图像。 I don't know why. 我不知道为什么 Here's the relevant bits of code I have so far. 这是我到目前为止拥有的相关代码。 I can post more if required. 如果需要,我可以发布更多。

**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>

This part works fine and bg_pic is displayed. 这部分工作正常,并显示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);
        }

    }

I want the button to work only if "Secret" is typed in. 我希望按钮仅在输入“ 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);
    }    

I know I'm probably doing something wrong here. 我知道我可能在这里做错了。

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>

I would expect test_pic to be the background of the page that is displayed when the button is pressed. 我希望test_pic是按下按钮时显示的页面的背景。 But its not. 但事实并非如此。 The background is just white. 背景是白色的。

I realize this is probably a trivial question but can someone please help me out? 我意识到这可能是一个琐碎的问题,但是有人可以帮我吗? I would really appreciate it. 我真的很感激。

Thank you. 谢谢。

Why you used setContentView() 2 times in DisplayMessageActivity.java. 为什么在DisplayMessageActivity.java中两次使用setContentView()。 Remove the secound setcontentview() and assign some id for the textview in activity_display_message.xml. 删除第二个setcontentview()并在activity_display_message.xml中为textview分配一些ID。 Here is the code to assign id for textview. 这是为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"/>

And get the textview with that id reference like 并获得具有该ID参考的textview

TextView textView = (TextView)findViewById(R.id.mytv);

And use this textview reference to show message from the previous activity like this 并使用此textview参考显示来自上一个活动的消息,如下所示

textView.setText(message);

I hope this will helpful for you. 希望对您有帮助。

Change code 变更密码

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 = (TextView)findViewByID(R.id.textMessage)
        textView.setTextSize(24);
        textView.setText(message);
        }

and also put id for textview 并为id设置id

<TextView android:id="@+id/textMessage"
  android:text="@string/hello_world" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

删除此setContentView(textView);

Why you have wrote setContentView(textView); 为什么写了setContentView(textView); line? 线?

Just remove above given line of the code, and you are done. 只需删除上面给定的代码行,即可完成。

try like this, 这样尝试

<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:id="@+id/txtmsg"
            android:text="@string/hello_world" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

    </RelativeLayout>


    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 = (TextView) findViewById(R.id.txtmsg);
            textView.settext(message);
        }    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM