2012-01-30 8 views
2

두 개의 단추 (하나는 다음 뉴스, 다른 하나는 이전 뉴스)를 사용하고 있습니다. 내 활동에서 첫 번째 소식을 전하면서 ​​버튼이 회색 이미지로 이미지를 설정해야하고 앞으로 나아갈 때 이전 뉴스 버튼이 회색 이미지에서 색칠 된 이미지로 바뀌어야합니다. 마찬가지로 마지막 뉴스에 도달하면 다음 버튼이 회색 이미지로 변경됩니다.단추의 배경 이미지 변경

이것은 내 xml 파일입니다.

public void onClick(View v) { 

     if (v == btnPrevNews && newsId > 0) { 
      if (newsId > 0) { 
       newsId--; 
       putDetailNewsinView(newsId); 
      } else if (newsId == 0) { 
       btnPrevNews 
         .setBackgroundResource(R.drawable.disable_arrow_left); 
       btnPrevNews.setEnabled(false); 
      } 
      // btnPrevNews.setVisibility(View.INVISIBLE); 

     } else if (v == btnNextNews && newsId < newsListDetail.size() - 1) { 
      if (newsId < newsListDetail.size() - 1) { 
       newsId++; 
       putDetailNewsinView(newsId); 
       if(newsId == 0) 
        btnNextNews.setBackgroundDrawable(getResources().getDrawable(
          R.drawable.disable_arrow_right)); 
      } else if (newsId == newsListDetail.size()) { 
       // btnNextNews.setVisibility(View.INVISIBLE); 
       btnNextNews.setBackgroundDrawable(getResources().getDrawable(
         R.drawable.disable_arrow_right)); 
       // btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right); 
       btnNextNews.setEnabled(false); 
      } 
     } 

    } 

이 저를 도와주십시오

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFFFF" 
    > 

    <TextView 
     android:id="@+id/tv_submitDate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="Medium Text" 
     android:textColor="#000000" 
     android:padding="5dp"/> 

    <TextView 
     android:id="@+id/tv_headline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_submitDate" 
     android:text="Medium Text" 
     android:textColor="#000000" 
     android:textStyle="bold" 
     android:padding="5dp"/> 

    <View 
     android:id="@+id/viewSeperator" 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:layout_below="@+id/tv_headline" 
     android:background="#FF909090" /> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btn_relative_layout" 
     android:layout_below="@+id/viewSeperator" android:padding="10dp"> 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TextView 
       android:id="@+id/tv_detailNews" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Medium Text" 
       android:textColor="#000000" /> 
     </LinearLayout> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/btn_relative_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#DCDCDC" 
     android:padding="10sp" > 


     <Button 
      android:id="@+id/btn_prevNews" 
      android:layout_width="120sp" 
      android:layout_height="40sp" 
      android:layout_weight="0.5" 
      android:layout_marginRight="5sp" 
      android:background="@drawable/arrow_left" 
      android:clickable="true" /> 


     <Button 
      android:id="@+id/btn_nextNews" 
      android:layout_width="120sp" 
      android:layout_height="40sp" 
      android:layout_marginLeft="5sp" 
      android:layout_weight="0.5" 
      android:background="@drawable/arrow_right" 
      android:clickable="true" /> 
     </LinearLayout> 


</RelativeLayout> 

이것은 내 버튼의 이미지를 변경하려고 곳입니다.

미리 감사드립니다.

+1

안녕과에 오신 것을 환영합니다! 지금까지 가지고있는 것을 보여주십시오. 코드 또는 XML에서 버튼을 정의 했습니까? – WarrenFaith

답변

1

버튼을 눌렀을 때 실행될 메소드가 있다고 가정합니다. 변화처럼, 당신이 당신의 버튼으로 모든 종류의 것들을 할 수있는 지금 Button mybutton = (Button) findViewById(R.id.yourid);

: 당신은 이러한 버튼에 액세스 할 수 있습니다, 두 버튼에게 자료 : 그냥 회색이 버튼의 배경을 변경하는 논리를 다음을 사용하여 추가 배경.

편집 : 문제는 그림에 놓이게되지만 내가 너라면, 몇 가지 중단 점을 설정하고 단계별로 디버그합니다.

이 기능을 테스트하기 위해 일부 코드를 해킹 했으므로 정상적으로 작동합니다.

package my.namespac; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class SomeTestProjectActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button button = (Button)findViewById(R.id.btn_prevNews); 

     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Button b = (Button)v; 
       v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala)); 

      } 
     }); 
    } 
} 

그리고 레이아웃 XML이 : 여기있다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


      <Button 
      android:id="@+id/btn_prevNews" 
      android:layout_width="120sp" 
      android:layout_height="40sp" 
      android:layout_weight="0.5" 
      android:layout_marginRight="5sp" 
      android:background="@drawable/ic_launcher" 
      android:clickable="true" /> 
</LinearLayout> 
+0

나는 이것을 알고있다, 나는 이것을 사용하고있다. 내 코드에서는이 문을 사용하지만 이미지를 변경하지는 않습니다. btnNextNews.setBackgroundDrawable (의 GetResources() getDrawable ( \t \t \t \t \t \t \t R.drawable.disable_arrow_right).); – aiRbornE

+0

예. 고마워. – aiRbornE