Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更改以前创建的自定义视图的一个属性?_Java_Android_View_Android Custom View - Fatal编程技术网

Java 如何更改以前创建的自定义视图的一个属性?

Java 如何更改以前创建的自定义视图的一个属性?,java,android,view,android-custom-view,Java,Android,View,Android Custom View,我有一个创建自定义视图的类。我称之为植物景观。它们每个都有以下属性:名称、程度、信息和图像。我想更改一个名为“Test”的类的信息文本,例如,在单击另一个类中的按钮之后。我试图为这些自定义视图设置标记,但我还没有真正理解它是如何工作的 PlantView.java public class PlantView extends FrameLayout { //Views private UnderlinedTextView nameView; private TextView infoView;

我有一个创建自定义视图的类。我称之为植物景观。它们每个都有以下属性:名称、程度、信息和图像。我想更改一个名为“Test”的类的信息文本,例如,在单击另一个类中的按钮之后。我试图为这些自定义视图设置标记,但我还没有真正理解它是如何工作的

PlantView.java

public class PlantView extends FrameLayout {

//Views
private UnderlinedTextView nameView;
private TextView infoView;
private TextView degreeView;
private CircleImageView imageView;
private LinearLayout planteBg;

//Attributes
private String nameText;
private String infoText;
private String degreeText;
private Drawable plantImage;

private boolean isExtended = false;

/****************
 * CONSTRUCTEURS
 ***************/

/**
 * Constructeur de la classe.
 *
 * @param context the context.
 */
public PlantView(@NonNull Context context) {
    super(context);
    obtainStyledAttributes(context, null, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context the context.
 * @param attrs   the attributes from the layout.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    obtainStyledAttributes(context, attrs, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context      the context.
 * @param attrs        the attributes from the layout.
 * @param defStyleAttr the attributes from the default style.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    obtainStyledAttributes(context, attrs, defStyleAttr);
    init();
}


/**********
* FONCTIONS
***********/

//Fonction utile pour récupérer les attributs si l'on ajoute une "PlantView" directement en XML
private void obtainStyledAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
    if (attrs != null) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlantView, defStyleAttr, 0);
        nameText = typedArray.getString(R.styleable.PlantView_name);
        infoText = typedArray.getString(R.styleable.PlantView_info);
        degreeText = typedArray.getString(R.styleable.PlantView_degree);
        plantImage = typedArray.getDrawable(R.styleable.PlantView_android_src);

    }
}
//Fonction permettant d'initialiser la vue
private void init() {
    inflate(getContext(), R.layout.plantview, this);
    nameView = findViewById(R.id.nomPlante);
    infoView = findViewById(R.id.info);
    degreeView = findViewById(R.id.degree);
    imageView = findViewById(R.id.image);
    planteBg = findViewById(R.id.plante);

    setupView();
}
//Fonction permettant d'"installer" la vue
private void setupView() {

    nameView.setText(nameText);
    infoView.setText(infoText);
    degreeView.setText(degreeText +" °C");
    imageView.setImageDrawable(plantImage);
    planteBg.setOnClickListener(new OnClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {

            if(isExtended){
                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));


                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = LayoutParams.WRAP_CONTENT;

                planteBg.setLayoutParams(params);
                isExtended = false;

            }else if(isExtended == false) {

                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));

                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = 500;
                planteBg.setLayoutParams(params);
                isExtended = true;
            }
        }
    });
}

/**
 * Fonctions permettant de changer les attributs
 * de la vue par programmation
 */
public void setName(String name) {
    nameView.setText(name);
}
public void setInfo(String info){
    infoView.setText(info);
}
public void setDegree(String degree){
    degreeView.setText(degree+" °C");
}
public void setImage(Bitmap image){
    imageView.setImageBitmap(image);
}

invalidate()是什么意思?我想我没有给你举个好例子。我想做的是:如果学位文本低于我从天气预报收到的某个值,我想更改信息文本。你明白吗?我设法获得了天气,但知道如何比较这些值以更改信息文本?下面是完整的代码:基本上它会导致视图重新绘制自身。是的,我明白。我想你想让它在你看来完成?只需将新学位与旧学位进行比较,如果更大,请调用setInfo()。
    On your setters after you've set the text call invalidate();
    public void setName(String name) {
    nameView.setText(name);
invalidate();
    }
    public void setInfo(String info){
        infoView.setText(info);
invalidate();
    }
    public void setDegree(String degree){
        degreeView.setText(degree+" °C");
invalidate();
    }
    public void setImage(Bitmap image){
        imageView.setImageBitmap(image);
invalidate();
    }