我的 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;
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();
}
}
在上面的代码textView.setText("Test");
中不起作用:它不会改变字段内容。我在 xml (@string/alert) 中设置的默认文本仍然显示!
好吧,看看你正在使用的图书馆,我发现了这个 .setContentHolder(new ViewHolder(view))
所以改变你的代码
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();
至
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句