2014-11-04 6 views
0

Android 개발에 새로 도입되었습니다. 텍스트 입력 필드와 버튼이있는 교육용 앱이 있습니다. 입력 된 텍스트가 단어와 일치하는 경우 button click은 일부 텍스트를 표시하는 새 페이지 (의도?)를 만듭니다.Android 버튼 이미지를 클릭하면 배경 이미지가 사라집니다.

내가 겪고있는 문제는 버튼 클릭으로 생성 된 새 페이지에는 기본 페이지와 동일한 배경 이미지가 없다는 것입니다. 나는 이유를 모른다. 지금까지 내가 가지고있는 관련 코드 비트는 다음과 같습니다. 필요한 경우 더 게시 할 수 있습니다.

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

    } 

나는 "비밀"에 입력되어있는 경우 버튼 만 작동합니다.

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이 버튼을 눌렀을 때 표시되는 페이지의 배경이 될 것으로 기대합니다. 그러나 그렇지 않습니다. 배경은 흰색뿐입니다.

나는 이것이 아마도 사소한 질문이라는 것을 알고 있지만 누군가 나를 도울 수 있습니까? 나는 정말로 감사 할 것입니다.

감사합니다.

+0

당신의 텍스트 뷰에 "android : background ="@ drawable/test_pic "도 있습니다. 따라서 액티비티 배경은 textview의 것과 같습니다. –

+1

환영 활동에서 bg_pic을 배경으로 사용하고 DisplayMessageActivity를 test_pic을 배경으로 사용하여 배경을 동일하게 두 활동 모두에 대해 –

답변

3

텍스트 뷰

<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"/> 
0

이것을 제거하십시오. setContentView (textView);

0

쓴 이유 setContentView (textView); 라인?

위 코드 줄을 제거하면 완료됩니다. 당신이 DisplayMessageActivity.java에서() 2 번 된 setContentView를 사용하는 이유

1

변경 코드

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

도는

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

이 같은 시도에 대한 ID를 넣어. secound 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"/> 

그리고

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

같은 해당 ID를 참조하여 텍스트 뷰를 얻을 그리고 희망이

textView.setText(message); 

처럼 이전 활동에서 메시지를 표시하려면이 텍스트 뷰 참조를 사용 이것은 당신에게 도움이 될 것입니다.

+0

@MK 왜 항상 모든 코드를 제공합니까? 설명이나 힌트를 제공하기 만하면됩니다. 모든 코드를 제공 할 필요가 없습니다. 적절한 대답이 아닙니다. –

관련 문제