简体   繁体   English

如何为 ImageView 创建一个 id

[英]How to create an id for an ImageView

I am trying to create a reminder application.我正在尝试创建一个提醒应用程序。 If the user presses a button he can add a reminder, but I can't seem to get him to delete it.如果用户按下按钮,他可以添加提醒,但我似乎无法让他删除它。 In fact, I created a method called add a reminder and which creates a new ImageView when we select a day and write text in a Text.事实上,我创建了一个名为添加提醒的方法,当我们每天 select 并在 Text 中写入文本时,它会创建一个新的 ImageView。 But I can't delete the ImageView because I don't know how to find it.但是我无法删除 ImageView 因为我不知道如何找到它。

I have already thought about putting a variable one which is equal to the ImageView, then if we recreate a callback to put as name two with if (one! = Null) {ImageView two = add a callback} .我已经考虑过放置一个等于 ImageView 的变量one ,然后如果我们重新创建一个回调以使用if (one! = Null) {ImageView two = add a callback}作为名称two But if I do that I have to write each variable manually, and therefore it's a bit complicated for example to write 2000 times each number in letters if there are 20,000 reminders.但是如果我这样做,我必须手动编写每个变量,因此如果有 20,000 个提醒,则用字母写每个数字 2000 次有点复杂。 Can tell me how I can find each ImageView I create so I can just delete it?能告诉我如何找到我创建的每个 ImageView 以便我可以删除它吗?

By the way is what you could tell me how to change the size of an image with java code.顺便说一句,您可以告诉我如何使用 java 代码更改图像的大小。 I have already tried setMinimumHeight and setMaxHeight but it does not work.我已经尝试过setMinimumHeightsetMaxHeight但它不起作用。

package com.solal.roundbutton;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {


    public ImageView  nouvelleimage( int resources){

        ImageView imageView = new ImageView(this);
        LinearLayout ll = (LinearLayout)findViewById(R.id.linear);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imageView.setImageResource(resources);
        imageView.setMinimumHeight(100);
        imageView.setMaxHeight(300);
        ll.addView(imageView,  lp);
        return imageView;
    }

Button button, supprimer;
int numero;
String dd;



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

        button = findViewById(R.id.button);
        supprimer = findViewById(R.id.supprimer);
        numero = 0;

        dd = String.valueOf(numero);

TextView  string = new TextView(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ImageView un = nouvelleimage( R.drawable.youstlogo);
            }
        });

     ImageView image1 = nouvelleimage(R.drawable.youstlogo);
        nouvelleimage( R.drawable.youstlogo);

        supprimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               
            }
        });
    }

    public void supprimerimage( ImageView imageView){
        LinearLayout ll = (LinearLayout)findViewById(R.id.linear);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ll.removeView(imageView);

    }
}

You can create a list in your class:您可以在 class 中创建一个列表:

public class MainActivity extends AppCompatActivity {
  List<ImageView> images = new ArrayList<>();
  ...

Then in the button click listener:然后在按钮点击监听器:

button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    ImageView image = nouvelleimage( R.drawable.youstlogo);
    images.add(image):
  }
});

Now if you want to delete the n-th image, you can call:现在,如果要删除第 n 个图像,可以调用:

ImageView image = images.remove(n - 1):
deleteImage(image):

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

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