2013-05-26 1 views
1

어제 나는 내 버튼이 비활성화 및 활성화 한 후에도 시각적 인 피드백을주지 못하는 문제를 해결하려고했습니다. my problem배경색을 변경 한 후 테두리없는 단추에서 피드백을 얻으려면 어떻게해야합니까? selectableItemBackground

그러나이 단추가 어떻게 표시되는지 내 대답에 맞지 않았습니다. 결국 나는 스톡 버튼을 사용하는 대신 drawables로 내 자신의 버튼을 만든다. 하지만 버튼을 바꾸고 싶지 않다는 단점이 있습니다. 그래서 나는 당신의 도움으로 다른 방법을 찾을 수 있기를 바랍니다. 내가 API (14) 그래서를 일하고 있어요

 <Button 
      android:id="@+id/buttonEat" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?android:attr/selectableItemBackground" 
      android:paddingBottom="@dimen/padding_size" 
      android:paddingTop="@dimen/padding_size" 
      android:text="@string/button_eat" 
      android:textColor="@color/white" 
      android:textColorHint="@color/white" 
      android:textSize="@dimen/text_size" /> 

: XML에 How to create standard Borderless buttons

내 버튼 :

알 수있는 importaned 것은 내가 방법으로 주식 버튼을 변경 그것이 그녀를 discriped된다는 것이다 API 11 isue는 문제가 아닙니다.

두 가지 방법으로 마녀가 내 버튼을 사용 중지하고 사용하도록 설정했습니다. 활성화 한 후에도 여전히 재미 있지만 피드백을 제공하지 않습니다.

private void startSleeping() 
{ 
    editorState.putBoolean("SLEEPING", true); 
    editorState.commit(); 

    buttonDrink.setEnabled(false); 
    buttonEat.setEnabled(false); 
    buttonWash.setEnabled(false); 
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray)); 
    buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray)); 
    buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray)); 
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange)); 
    buttonWash.setTextColor(getResources().getColor(R.color.lightgray)); 
    buttonDrink.setTextColor(getResources().getColor(R.color.lightgray)); 
    buttonEat.setTextColor(getResources().getColor(R.color.lightgray)); 
    buttonSleep.setTextColor(getResources().getColor(color.black)); 
} 

private void stopSleeping() 
{ 
    editorState.putBoolean("SLEEPING", false); 
    editorState.commit(); 

    buttonDrink.setEnabled(true); 
    buttonEat.setEnabled(true); 
    buttonWash.setEnabled(true); 

// **Her is the problem** 
// **if tried diffrent things** 

// **First try: (brings back the old color but not the feedback)** 
//  buttonDrink.setBackgroundColor(android.R.attr.selectableItemBackground); 
// **Second try: (App crashes. Why? i don't know. The discription of selectableItemBackground says it is a drawable...)** 
//buttonDrink.setBackgroundDrawable(getResources().getDrawable(android.R.attr.selectableItemBackground)); 
// **Third try: (eclips isn't accepting this because the attribut is a int and not a drawable)** 
//buttonDrink.setBackgroundDrawable(android.R.attr.selectableItemBackground); 
//**Fourth try: (brings back a feedback but changes the lock, feedback color...)** 
TypedArray a = getBaseContext().obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground}); 
    Drawable backdraw = a.getDrawable(0); 
    buttonDrink.setBackgroundDrawable(backdraw); 

    buttonEat.setBackgroundColor(android.R.attr.selectableItemBackground); 
    buttonWash.setBackgroundColor(android.R.attr.selectableItemBackground); 
    buttonSleep.setBackgroundColor(android.R.attr.selectableItemBackground); 
    buttonWash.setTextColor(getResources().getColor(R.color.white)); 
    buttonDrink.setTextColor(getResources().getColor(R.color.white)); 
    buttonEat.setTextColor(getResources().getColor(R.color.white)); 
    buttonSleep.setTextColor(getResources().getColor(R.color.white)); 
} 

그러나 selectableItemBackground에서 funktionality을이 버튼을 돌려 줄 수있는 방법이 있어야합니다.

배경색이 흐릿한 것 같습니다. 누구든지 아이디어가 있습니까? pls 알려주세요

답변

1

코드에서 단추의 배경색을 변경하는 대신 사용자 지정 드로어 블을 만들 수 있으며 다양한 상태 (활성화, 누름, ...)에 대한 색을 정의 할 수 있습니다.

<solid 
    android:color="#ffffff"/> 
<stroke 
    android:width="1dp" 
    android:color="#000000" /> 
<corners 
    android:radius="4dp" /> 
<padding 
    android:left="10dp" 
    android:top="10dp" 
    android:right="10dp" 
    android:bottom="10dp" /> 

지금 당신이 설정할 수 있습니다 모양 태그

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" > 
     <shape> 
      <!-- define style for disabled state here --> 
     </shape> 
    </item> 
    <item android:state_pressed="true" > 
     <shape> 
      <!-- define style for pressed state here --> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <!-- define style for normal state here --> 
     </shape> 
    </item> 
</selector> 

예를 들어 내용 : 그냥/입술에서 XML 파일을 생성/당김 폴더를 사용자 정의 드로어 블을 만들려면 여기에 몇 가지 샘플 내용입니다 이 drawable은 레이아웃 xml 파일의 버튼의 배경으로 사용됩니다. 그런 다음 버튼의 텍스트 색상 만 변경하면됩니다.

+0

나는 결코 그것을하지 않았기 때문에 나는 나의 자신의 주문 단추를 만들기에 대하여 죽었다. 하지만 당신의 코드 예제에 대해 생각해 보았습니다. 고마워, 지금 나는 새로운 것을 배웠고 계속 나아갈 수있다. :) – Spen

관련 문제