gpt4 book ai didi

java - Android : Show a list of color previews and pass associated id from color. xml

转载 作者:行者123 更新时间:2023-12-01 11:04:04 28 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,其中有一个已插入 color.xml 的颜色列表。这些颜色适用于从 Activity 中创建的注释。因此,用例是用户输入标签、文本并从 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);
}
});

}
}

任何帮助都会很好。多谢。 :-)

编辑

如下图所示,当用户单击该图像时,我会在 Activity 中获取颜色 ID。

Color image

最佳答案

只需使用 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
}
});

关于java - Android : Show a list of color previews and pass associated id from color. xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com