Android:显示颜色预览列表并从color.xml传递相关的id

时间:2015-10-14 13:57:46

标签: java android colors

我正在开发一个Android应用程序,其中我有一个我在color.xml中插入的颜色列表。这些颜色用于从活动创建的注释。因此,用例是用户输入标签,文本并从color.xml中的可用颜色中选择颜色。

请注意,我不是在寻找颜色选择器,因为颜色已被选中,用户只能从这些中选择

所以,我想向用户显示一个颜色列表作为小框中的预览(剩下的空间),点击后,会出现一个下拉菜单,显示color.xml中插入的名称和相关的预览。

我不知道哪种类型的容器或实现此任务需要什么。我将发布可用的代码和颜色。请指导我如何进行

color.xml:只有用户可以拥有的颜色:

   <color name="noteAqua">#1abc9c</color>
    <color name="noteBlue">#3498db</color>
    <color name="noteDarkBlue">#34495e</color>
    <color name="noteDeepBlue">#0071c1</color>
    <color name="noteDeepRed">#c00000</color>
    <color name="noteGreen">#2ecc71</color>
    <color name="noteGrey">#95a5a6</color>
    <color name="noteOrange">#e67e22</color>
    <color name="notePink">#ff56bb</color>
    <color name="notePurple">#9b59b6</color>
    <color name="noteRed">#e74c3c</color>
    <color name="noteYellow">#f1c40f</color>

create_note.xml:如果你们有运行android的IDE,你可以复制粘贴下面的XML,你可以注意到,我在右上角留下了一个空白,我想插入一个列表颜色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <EditText
        android:layout_width="315dp"
        android:layout_height="wrap_content"
        android:id="@+id/noteTagAddText"
        android:gravity="center_vertical|center_horizontal" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/noteTextAddText"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.97"
        android:gravity="top" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="191dp"
            android:layout_height="wrap_content"
            android:text="@string/saveNoteString"
            android:id="@+id/createNoteButton"
            android:layout_gravity="right" />

        <Button
            android:layout_width="191dp"
            android:layout_height="wrap_content"
            android:text="@string/cancelSaveString"
            android:id="@+id/cancelCreateButton" />
    </LinearLayout>
</LinearLayout>

CreateNoteActivity类:

public class CreateNoteActivity extends Activity {
 EditText noteTag, noteText;
    Button saveButton, cancelButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_note);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            groupAccountId = extras.getLong("groupid");
            canvasId = extras.getInt("canvasid");
            sectionid = extras.getInt("sectionid");
        }

        noteTag = (EditText) findViewById(R.id.noteTagEdit);
     //   noteTag.setText(restNote.getMnotetag());

        noteText = (EditText) findViewById(R.id.noteTextEdit);
       // noteText.setText(Html.fromHtml(restNote.getMnotetext()));

        noteText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s){
       //     restNote.setMnotetext(s.toString());
            }
        });


        noteTag.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
             //   restNote.setMnotetag(s.toString());
            }

        });
        saveButton = (Button) findViewById(R.id.saveNoteButton);
        cancelButton = (Button) findViewById(R.id.cancelEditButton);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RestNote saveNote = new RestNote();
          //      saveNote.setMnotecolor(restNote.getMnotecolor());
                saveNote.setMnotetag(noteTag.getText().toString());
                saveNote.setMnotetext(noteText.getText().toString());
           //     saveRestNoteOnServer(restNote);

                Intent intent = new Intent(getApplicationContext(),GroupSectionActivity.class);
                intent.putExtra("groupid", groupAccountId);
                intent.putExtra("canvasid", canvasId);
                intent.putExtra("sectionid",sectionid);
                startActivity(intent);
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),GroupSectionActivity.class);
                intent.putExtra("groupid", groupAccountId);
                intent.putExtra("canvasid", canvasId);
                intent.putExtra("sectionid",sectionid);
                startActivity(intent);
            }
        });

    }
}

任何帮助都会很好。非常感谢。 : - )

编辑

如下图所示,当用户点击图片时,我会在活动中获得颜色ID。

Color image

1 个答案:

答案 0 :(得分:1)

只需使用ImageView并将背景属性设置为您的颜色,就像这样;

<ImageView
     android:layout_width="50dp"
     android:layout_height="50dp"
     android:background="@color/yourcolour"/>

然后,您可以在此ImageView上设置单击侦听器,并在单击后执行任何操作。

yourImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 // do something
            }
        });