버튼을 회색으로 표시하는 방법은 무엇입니까?
아래와 같이 정의 된 버튼이 있습니다. 비활성화하려면을 사용 my_btn.setEnabled(false)
하지만 회색으로 표시하고 싶습니다. 어떻게 할 수 있습니까?
감사
<Button android:id="@+id/buy_btn" style="@style/srp_button" />
스타일 / srp_button
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">14sp</item>
<item name="android:typeface">serif</item>
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
drawable / btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/pink" />
<corners android:radius="6dp" />
</shape>
btn_defaut.xml
선택기로 3 개 또는 4 개의 상태를 제공해야합니다 .
- 눌러 진 상태
- 기본 상태
- 초점 상태
- 활성화 상태 (잘못된 표시가있는 비활성화 상태, 주석 참조)
그에 따라 주에 대한 효과와 배경을 제공합니다.
자세한 설명은 다음과 같습니다. 색상이 다른 표준 Android 버튼
알파를 설정하여 (반투명하게) 비활성화 된 것으로 표시 할 수도 있습니다. 버튼 배경이 이미지이고 상태를 생성하지 않으려는 경우 특히 유용합니다.
button.setAlpha(.5f);
button.setClickable(false);
업데이트 : 이 프리 Kotlin을 썼고 신인이었을 때. "빠르고 더러운"솔루션에 가깝지만 전문적인 환경에서는 권장하지 않습니다.
현재 상태 목록을 만들지 않고도 모든 버튼 /보기에서 작동하는 일반적인 솔루션을 원한다면 Kotlin 확장 프로그램을 만들 것입니다.
fun View.disable() {
getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
setClickable(false)
}
Java에서는 정적 util 함수와 유사한 작업을 수행 할 수 있으며보기를 변수로 전달하면됩니다. 깨끗하지는 않지만 작동합니다.
가장 쉬운 해결책은 여기에서 본 것처럼 버튼의 배경 이미지에 컬러 필터를 설정하는 것입니다.
다음과 같이 할 수 있습니다.
if ('need to set button disable')
button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
button.getBackground().setColorFilter(null);
내가 누군가를 도왔 으면 좋겠다 ...
All given answers work fine, but I remember learning that using setAlpha can be a bad idea performance wise (more info here). So creating a StateListDrawable is a better idea to manage disabled state of buttons. Here's how:
Create a XML btn_blue.xml in res/drawable folder:
<!-- Disable background -->
<item android:state_enabled="false"
android:color="@color/md_blue_200"/>
<!-- Enabled background -->
<item android:color="@color/md_blue_500"/>
Create a button style in res/values/styles.xml
<style name="BlueButton" parent="ThemeOverlay.AppCompat">
<item name="colorButtonNormal">@drawable/btn_blue</item>
<item name="android:textColor">@color/md_white_1000</item>
</style>
Then apply this style to your button:
<Button
android:id="@+id/my_disabled_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/BlueButton"/>
Now when you call btnBlue.setEnabled(true)
OR btnBlue.setEnabled(false)
the state colors will automatically switch.
Set Clickable as false and change the backgroung color as:
callButton.setClickable(false);
callButton.setBackgroundColor(Color.parseColor("#808080"));
You should create a XML file for the disabled button (drawable/btn_disable.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/grey" />
<corners android:radius="6dp" />
</shape>
And create a selector for the button (drawable/btn_selector.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_disable" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_default" android:state_enabled="true"/>
<item android:drawable="@drawable/btn_default" android:state_pressed="false" />
</selector>
Add the selector to your button
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_selector</item>
</style>
Button button = (Button)findViewById(R.id.buy_btn);
button.setEnabled(false);
I used this code for that:
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
profilePicture.setColorFilter(filter);
u can make a grey picture and put in into your drawable and add this into your code.
button.setBackgroundResource(R.drawable.grey);
easy,simple,but because of using picture it gets a little more space.
참고URL : https://stackoverflow.com/questions/8743120/how-to-grey-out-a-button
'development' 카테고리의 다른 글
파이썬에서 이전 인쇄를 stdout에 덮어 쓰는 방법은 무엇입니까? (0) | 2020.08.27 |
---|---|
Visual Studio Editor에서 시각적 "점"을 비활성화하는 방법 (0) | 2020.08.27 |
Android ListView의 하단 구분선 제거 (0) | 2020.08.27 |
iOS : 코드에서 app-info.plist 변수에 액세스 (0) | 2020.08.27 |
UIFont-시스템 얇은 글꼴을 얻는 방법 (0) | 2020.08.27 |