0

Я пытаюсь имитировать селектор xml (первая кнопка) с помощью java (вторая кнопка).Как удалить пустую кнопку setBackgroundTintList()

Допустим, у меня есть этот onCreate() Java код в MainActivity.java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button secondButton = (Button) findViewById(R.id.buttonJavaSelector); 
    int[][] hole_states = new int[][] { 

      new int[]{android.R.attr.state_pressed}, 
      new int[]{} 
    }; 
    int[] hole_colors = new int[] { 
      ContextCompat.getColor(this, android.R.color.white), 
      ContextCompat.getColor(this, android.R.color.holo_green_light) 
    }; 
    ColorStateList holeStateList = new ColorStateList(hole_states, hole_colors); 
    //secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light)); 
    secondButton.setBackgroundTintList(holeStateList); 
    secondButton.setHighlightColor(Color.RED); 
} 

Тогда activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.blogspot.diannaoxiaobai.tmptesting.MainActivity"> 

    <LinearLayout 
     android:id="@+id/layout_sign_in" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/yes_part" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <Button 
       android:id="@+id/buttonXmlSelector" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/button_ripple_light" 
       android:text="Cancel (Xml Selector)" 
       android:textColor="@android:color/holo_blue_light" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/no_part" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <Button 
       android:id="@+id/buttonJavaSelector" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Sign In (Java Selector)" 
       android:textColor="@android:color/holo_blue_light" /> 

     </LinearLayout> 

    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

А потом drawable/button_ripple_light.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@android:color/white" /> 
    <item android:drawable="@android:color/holo_green_light" /> 
</selector> 

скриншот Выход:

enter image description here

Если я нажал обе кнопки, и возможность стать белым цветом:

enter image description here

Но я не хочу, отступов вокруг кнопки 2. Я искал много ответов в S/O, они всегда предлагают изменить цвет фона для удаления (кто-то сказал, что это тень, а не дополнение). Поэтому я раскомментировать эту строку в MainActivity.java выше (Обратите внимание, что я установил красный цвет здесь):

secondButton.setBackgroundColor(this.getResources().getColor(android.R.color.holo_red_light)); 

Теперь обивка больше не существует, но ничего не происходит (т.е. становятся белыми), когда я нажимаю вторую кнопку:

enter image description here

setBackgroundColor() для удаления прокладки работает неправильно. Что я должен сделать, чтобы удалить вторую кнопку в этом случае?

ответ

1

Попробуйте

StateListDrawable holeStateList = new StateListDrawable(); 
    holeStateList.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(ContextCompat.getColor(this, 
      android.R.color.white))); 
    holeStateList.addState(new int[]{}, new ColorDrawable(ContextCompat.getColor(this, 
      android.R.color.holo_green_light))); 

    secondButton.setBackground(holeStateList);