簡體   English   中英

單擊按鈕后,Android背景圖像消失

[英]Android Background image disappears after button click

Android開發的新手。 我有一個具有文本輸入字段和按鈕的培訓應用程序。 如果輸入的文本與單詞匹配,則button click會創建一個新頁面(意圖?),該頁面顯示一些文本。

我遇到的問題是,通過按鈕單擊創建的新頁面沒有與主頁相同的背景圖像。 我不知道為什么 這是我到目前為止擁有的相關代碼。 如果需要,我可以發布更多。

**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參考顯示來自上一個活動的消息,如下所示

textView.setText(message);

希望對您有幫助。

變更密碼

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);
        }

並為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);

為什么寫了setContentView(textView); 線?

只需刪除上面給定的代碼行,即可完成。

這樣嘗試

<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