猿问

无法以编程方式更改android布局文件元素的值

我的Android项目中有一个Java类,用于创建自定义对话框。但是,当我想为自定义对话框布局文件设置文本或其他属性时,我做不到!


当我使用setText更改活动或自定义对话框java类中的对话框布局的textView时,不会发生任何更改。


这是我的自定义对话框布局文件


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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:background="@color/dark_primary"

android:layout_height="match_parent">


<TextView

    android:id="@+id/textView"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginLeft="24dp"

    android:layout_marginStart="24dp"

    android:layout_marginTop="8dp"

    android:text="@string/alert"

    android:textSize="@dimen/alert_title_font_size"

    android:textColor="@android:color/white"

    android:fontFamily="@font/berlin_regular"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这是我的对话框生成器Java类:


package com.x.Dialog;


import android.app.Activity;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.TextView;


import com.orhanobut.dialogplus.DialogPlus;

import com.orhanobut.dialogplus.OnItemClickListener;

import com.orhanobut.dialogplus.ViewHolder;

import com.x.x.R;


public class CustomAlertDialog {


public Context context;

public Activity activity;

public TextView textView;


在上面的代码textView.setText("Test");中不起作用:它不会更改字段内容。我在xml(@ string / alert)中设置的默认文本仍然显示!


阿晨1998
浏览 113回答 1
1回答

侃侃无极

好的,所以看着您正在使用的图书馆,我发现了这个 .setContentHolder(new ViewHolder(view))因此从更改您的代码DialogPlus dialog = DialogPlus.newDialog(this.context)&nbsp; &nbsp; &nbsp; &nbsp; .setContentHolder(viewHolder)&nbsp; &nbsp; &nbsp; &nbsp; .setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(DialogPlus dialog, Object item, View view, int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .setExpanded(true)&nbsp; // This will enable the expand feature, (similar to android L share dialog)&nbsp; &nbsp; &nbsp; &nbsp; .create();至&nbsp; DialogPlus dialog = DialogPlus.newDialog(this.context)&nbsp; &nbsp; &nbsp; &nbsp; .setContentHolder(new ViewHolder(dialogView ))&nbsp; &nbsp; &nbsp; &nbsp; .setOnItemClickListener(new OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(DialogPlus dialog, Object item, View view, int position) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; .setExpanded(true)&nbsp; // This will enable the expand feature, (similar to android L share dialog)&nbsp; &nbsp; &nbsp; &nbsp; .create();
随时随地看视频慕课网APP

相关分类

Java
我要回答