[英]Can't change value of android layout file elements programatically
I have a java class in my android project that creates my custom dialog. 我的Android项目中有一个Java类,用于创建自定义对话框。 But when I want to set a text or another properties to my custom dialog layout file, I cant! 但是,当我想为自定义对话框布局文件设置文本或其他属性时,我做不到!
No change take place when I use setText to change textView of my dialog layout in my activity or in custom dialog java class. 当我使用setText更改活动或自定义对话框java类中的对话框布局的textView时,不会发生任何更改。
here is my custom dialog layout file 这是我的自定义对话框布局文件
<?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>
here is my dialog builder java class : 这是我的对话框生成器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;
public CustomAlertDialog(Context context, Activity activity, ViewHolder viewHolder){
this.context = context;
this.activity = activity;
View dialogView = LayoutInflater.from(context).inflate(R.layout.alert_dialog_1, null);
textView = dialogView.findViewById(R.id.textView);
textView.setText("Test");
DialogPlus dialog = DialogPlus.newDialog(this.context)
.setContentHolder(viewHolder)
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
}
})
.setExpanded(true) // This will enable the expand feature, (similar to android L share dialog)
.create();
dialog.show();
}
}
In the above codes textView.setText("Test");
在上面的代码中, textView.setText("Test");
does not work: it will not change the field content. 不起作用: 不会更改字段内容。 The default text that I set in xml (@string/alert) is still shown! 我在xml(@ string / alert)中设置的默认文本仍然显示!
ok so looking at the library you are using i found this .setContentHolder(new ViewHolder(view))
好的,所以看着您正在使用的库,我发现了这个.setContentHolder(new ViewHolder(view))
so change your code from 所以从
DialogPlus dialog = DialogPlus.newDialog(this.context)
.setContentHolder(viewHolder)
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
}
})
.setExpanded(true) // This will enable the expand feature, (similar to android L share dialog)
.create();
to 至
DialogPlus dialog = DialogPlus.newDialog(this.context)
.setContentHolder(new ViewHolder(dialogView ))
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
}
})
.setExpanded(true) // This will enable the expand feature, (similar to android L share dialog)
.create();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.