我需要为button.For示例设置不同的颜色,当我单击按钮时,它应该显示多种颜色,我应该从中选择一种颜色,该颜色应该应用于button.How可以吗?需要您的建议朋友提前谢谢
发布于 2015-09-02 14:23:00
您需要创建自定义的可绘制XML并设置此按钮的背景。
android:background="@drawble/your XML"
你可以通过这种方式设置按钮按下时的颜色和正常状态的.Try,希望能对你有所帮助。
发布于 2015-09-02 15:08:15
试试这个..。
button.setOnClickListener(new OnClickListener() {
void onClick(View v) {
// create the color picker dialog and display it.
ColorPickerDialog cDialog = new ColorPickerDialog(v.getContext(),new OnColorChangedListener()
{
void colorChanged(int color) {
mSelectedColor = color;
}
}),initialColor);
cDialog.show();
}
});“mSelectedColor”将是应用于按钮的颜色。
发布于 2015-09-02 16:13:10
试着让这个希望对你有用..
在您想要显示颜色选项按钮的单击侦听器上调用colorDialog(),且body_view id被替换为您的按钮id。
colorDialog():-
private void colorDialog()
{
LayoutInflater inflater = LayoutInflater.from(CreateNewNote.this);
View view = inflater.inflate(R.layout.color_lay, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CreateNewNote.this);
alertDialogBuilder.setTitle("Select Color");
alertDialogBuilder.setIcon(R.drawable.alert_toast);
alertDialogBuilder.setView(view);
Button btncol1=(Button)view.findViewById(R.id.col1);
Button btncol2=(Button)view.findViewById(R.id.col2);
Button btncol3=(Button)view.findViewById(R.id.col3);
Button btncol4=(Button)view.findViewById(R.id.col4);
Button btncol5=(Button)view.findViewById(R.id.col5);
Button btncol6=(Button)view.findViewById(R.id.col6);
final AlertDialog alert = alertDialogBuilder.create();
alert.show();
title_lay=(RelativeLayout)findViewById(R.id.toplayout);
body_view=(View)findViewById(R.id.body);
btncol1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color1);
alert.dismiss();
}
});
btncol2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color2);
alert.dismiss();
}
});
btncol3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color3);
alert.dismiss();
}
});
btncol4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color4);
alert.dismiss();
}
});
btncol5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color5);
alert.dismiss();
}
});
btncol6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
body_view.setBackgroundResource(R.color.body_color6);
alert.dismiss();
}
});
}color_lay.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<Button
android:id="@+id/col1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#F8BBD0" />
<Button
android:id="@+id/col2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#FFCCBC" />
<Button
android:id="@+id/col3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#E1BEE7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<Button
android:id="@+id/col4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#BBDEFB" />
<Button
android:id="@+id/col5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#C8E6C9" />
<Button
android:id="@+id/col6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:background="#FFF9C4" />
</LinearLayout>
https://stackoverflow.com/questions/32345687
复制相似问题