如何为ImageView创建ID

时间:2020-06-29 17:20:09

标签: java android

我正在尝试创建一个提醒应用程序。如果用户按下按钮,他可以添加一个提醒,但是我似乎无法让他删除它。实际上,我创建了一个名为添加提醒的方法,当我们选择一天并在“文本”中编写文本时,该方法会创建一个新的ImageView。但是我无法删除ImageView,因为我不知道如何找到它。

我已经考虑过要放置一个与ImageView相等的变量one,然后如果我们重新创建一个回调,以two作为名称if (one! = Null) {ImageView two = add a callback}放置。但是,如果这样做,我必须手动编写每个变量,因此,如果有20,000个提醒,则用字母写每个数字2000倍会有点复杂。能告诉我如何找到创建的每个ImageView,以便将其删除吗?

顺便说一句,您可以告诉我如何使用Java代码更改图像的大小。我已经尝试过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);

    }
}

1 个答案:

答案 0 :(得分:0)

您可以在班级中创建一个列表:

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

然后在按钮中单击侦听器:

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

现在,如果要删除第n张图像,可以致电:

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