简体   繁体   中英

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. But I can't delete the ImageView because I don't know how to find it.

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} . 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. Can tell me how I can find each ImageView I create so I can just delete it?

By the way is what you could tell me how to change the size of an image with java code. I have already tried setMinimumHeight and setMaxHeight but it does not work.

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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