简体   繁体   English

如何在 Android Studio 中将 integer 值分配给 imageView?

[英]How to assign an integer value to an imageView in Android Studio?

I am creating a feedback app where a user has to click one of the 5 imageViews (1-5 rating) based on his/her experience.我正在创建一个反馈应用程序,用户必须根据他/她的经验单击 5 个 imageViews(1-5 个评级)之一。 My primary aim is to extract the integer value of this rating from the imageView click and push it to a SQLite database.我的主要目标是从 imageView 点击中提取此评级的 integer 值并将其推送到 SQLite 数据库。

I am trying to use setTag() and getTag() but to no avail.我正在尝试使用setTag()getTag()但无济于事。 Any help would be much appreciated.任何帮助将非常感激。 Thanks in advance.提前致谢。

activity_main.xml - activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="99dp"
        android:layout_marginLeft="99dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="172dp"
        android:layout_marginRight="172dp"
        android:layout_marginBottom="151dp"
        android:text="Name"
        android:textSize="22sp"
        app:layout_constraintBottom_toTopOf="@+id/imageView1"
        app:layout_constraintEnd_toStartOf="@+id/editTextPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="172dp"
        android:layout_marginLeft="172dp"
        android:layout_marginTop="54dp"
        android:ems="10"
        android:hint="Full Name"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@+id/textViewName"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="92dp"
        android:layout_height="103dp"
        android:layout_marginStart="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="151dp"
        android:layout_marginEnd="47dp"
        android:layout_marginRight="47dp"
        android:tag="1"
        app:layout_constraintEnd_toStartOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewName"
        tools:srcCompat="@tools:sample/avatars" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="92dp"
        android:layout_height="103dp"
        android:layout_marginStart="60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginBottom="64dp"
        android:tag="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView3"
        tools:srcCompat="@tools:sample/avatars" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="92dp"
        android:layout_height="103dp"
        android:layout_marginStart="46dp"
        android:layout_marginLeft="46dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="64dp"
        android:tag="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.833"
        app:layout_constraintStart_toEndOf="@+id/imageView4"
        tools:srcCompat="@tools:sample/avatars" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="92dp"
        android:layout_height="103dp"
        android:layout_marginStart="48dp"
        android:layout_marginLeft="48dp"
        android:layout_marginBottom="63dp"
        android:tag="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView1"
        tools:srcCompat="@tools:sample/avatars" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="92dp"
        android:layout_height="103dp"
        android:layout_marginStart="53dp"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="49dp"
        android:layout_marginRight="49dp"
        android:tag="3"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintStart_toEndOf="@+id/imageView2"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="333dp"
        android:layout_marginLeft="333dp"
        android:layout_marginTop="41dp"
        android:layout_marginEnd="340dp"
        android:layout_marginRight="340dp"
        android:layout_marginBottom="75dp"
        android:text="Were you satisfied with our hygiene standards?"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/imageView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextPersonName" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java - MainActivity.java -

public class MainActivity extends AppCompatActivity {

    EditText name;
    ImageView oneStar;
    ImageView twoStar;
    ImageView threeStar;
    ImageView fourStar;
    ImageView fiveStar;
    Intent intent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.editTextPersonName);
        oneStar = (ImageView) findViewById(R.id.imageView1);
        oneStar.setTag(1);
        oneStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = name.getText().toString()+"\n";
                Toast.makeText(MainActivity.this, (Integer) oneStar.getTag(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Try to replace oneStar.getTag() with view.getTag()尝试用 view.getTag() 替换 oneStar.getTag()

If you want to get the tag, and display it as a Toast message, the instead of casting it into an Integer object convert it to String using.toString method.如果您想获取标签并将其显示为 Toast 消息,则不要将其转换为 Integer object 使用.toString 方法将其转换为字符串。

Second parameter of Toast.makeText is interger, but you can't pass any integer. Toast.makeText 的第二个参数是整数,但不能传递任何 integer。 It must be the resource Id of a string (R.string.your_string).它必须是字符串的资源 ID (R.string.your_string)。 Remove "(Integer)" and it should solve your problem.删除“(整数)”,它应该可以解决您的问题。

I don't know exactly what are your requirements and in case you missed it, there is a build in rating bar in Android.我不知道您的具体要求是什么,如果您错过了,Android 中有一个内置评级栏 You can check the tutorial here你可以在 这里查看教程

it is less code and less error prone if you just add this to ImageView: android:onClick="imageViewClick", and create a handler in MainActivity like this:如果您只是将其添加到 ImageView: android:onClick="imageViewClick" 并在 MainActivity 中创建一个像这样的处理程序,则代码更少,更不容易出错:

        public void imageViewClick(View view) {
            String username = name.getText().toString()+"\n";
            Toast.makeText(MainActivity.this, (Integer)view.getTag(),Toast.LENGTH_SHORT).show();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM