Android的颜色选择器开关按钮 [英] Android color selector with radio buttons

查看:1170
本文介绍了Android的颜色选择器开关按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一组单选按钮来选择颜色。事情是这样的:

I would like to create a group of radio buttons to pick a color. Something like this:

在这里输入的形象描述

我如何才能做到这样呢?我没有找到原始单选任何颜色属性。我必须创建一个自定义的控制?如果是的话,可以有人只是暗示我的基本步骤,所以我可以尝试一些新的研究?我很新到Android,并试图通过做学习...

How can I achieve something like this? I didn't find any color property on the original RadioButton. Do I have to create a custom control? If yes, can someone just hint me on the basic steps so I can try to some new research? I'm very new to Android, and trying to learn by doing...

推荐答案

您一定能尝试定制单选按钮,或者你可以简单地使用或夸大的观点来实现这种颜色选择器。

You can surely try custom radio buttons or you could simply use or inflate views to achieve this kind of color picker.

使用XML:你需要在绘制文件夹中创建两个绘制资源文件。首先是这样的,

with xml: you will need to create two drawable resource files in the drawable folder. First goes like this,

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#e91e63" />
<size
    android:width="48dp"
    android:height="48dp" />

当你还没有收到在视图(点击)任意点击这是适用的。当我们检测到点击第二个文件适用。

This is applicable when you haven't received any click on the view (clickable). The second file applies when we detect a click.

    xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#e91e63" />
<size
    android:width="53dp"
    android:height="53dp" />
<stroke
    android:width="5dp"
    android:color="#d2d1d2" />

现在,在活动中人们需要设置背景绘制到视图(无论是图像按钮或ImageView的)。这是这样的(只是一个例子):

Now, in the activity one needs to set the background drawable to the view (be it image button or imageview). This goes like this (just an example):

public class MainActivity extends AppCompatActivity {
private ImageButton img;
private boolean isSelected = false;

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

    img = (ImageButton) findViewById(R.id.img);
    img.setClickable(true);
    img.setBackground(getDrawable(R.drawable.unselected_circle));
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img.startAnimation(AnimationUtils.loadAnimation(getBaseContext(), android.R.anim.fade_in));
            if (isSelected) {
                isSelected = false;
                img.setBackground(getDrawable(R.drawable.unselected_circle));
            } else {
                isSelected = true;
                img.setBackground(getDrawable(R.drawable.selected_circle));
            }
        }
    });
}

}

和activity_main布局看起来是这样的:

and the activity_main layout looks something like this:

<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:id="@+id/viewGroup"
tools:context="com.android.empty.MainActivity">

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_margin="20dp"
    android:clickable="true"
    android:id="@+id/img"/>

但是,在该方法的一个最终会创建用于不同颜色的多个可绘。为了避免这种情况,我们可以通过编程创建可绘制,书写code和一次使用的setColor(INT颜色)方法,使用同样为不同的颜色:

However, with this method one will end up creating multiple drawables for different colors. To avoid that, we can create the drawables programmatically, writing code once and using the same for different colors using setColor(int color) method:

public class MainActivity extends AppCompatActivity {
private ImageButton img;
private boolean isSelected = false;

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

    final GradientDrawable unselected = new GradientDrawable();
    unselected.setShape(GradientDrawable.OVAL);
    unselected.setColor(Color.parseColor("#e91e63"));
    unselected.setSize(144, 144);

    final GradientDrawable selected = new GradientDrawable();
    selected.setShape(GradientDrawable.OVAL);
    selected.setColor(Color.parseColor("#E91E63"));
    selected.setSize(159, 159);
    selected.setStroke(15, Color.parseColor("#D2D1D2"));

    img = (ImageButton) findViewById(R.id.img);
    img.setBackground(unselected);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img.startAnimation(AnimationUtils.loadAnimation(getBaseContext(), android.R.anim.fade_in));
            if (isSelected) {
                isSelected = false;
                img.setBackground(unselected);
            } else {
                isSelected = true;
                img.setBackground(selected);
            }
        }
    });
}

}

结果看起来像这样

注意:这个例子告诉才达到一个选择喜欢在问题中提到的一个途径。要创建多个选择,需要使用LayoutInflater类膨胀的视图(图像按钮)。

这篇关于Android的颜色选择器开关按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆