У меня есть класс Java в моем проекте Android, который создает мой настраиваемый диалог. Но когда я хочу установить текст или другие свойства в свой файл макета настраиваемого диалогового окна, я не могу!
Никаких изменений не происходит, когда я использую setText для изменения textView моего макета диалогового окна в моем действии или в Java-классе настраиваемого диалогового окна.
вот мой собственный файл макета диалогового окна
<?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), все еще отображается!
проверьте, работает ли это для вас dialog.setView (dialogView) .. добавьте это перед dialog.show
@AdeelTurk Я использую другую библиотеку для создания этого диалога, и у нее нет этого метода (setView).
затем измените источник библиотеки или напишите свою собственную библиотеку, или спросите разработчика, который ее написал




Хорошо, посмотрев на библиотеку, которую вы используете, я нашел этот .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();
большое спасибо! он работал отлично. но не могли бы вы объяснить, в чем разница между тем, как я установил макет для конструктора диалогов, и тем, как вы это сделали ?!
вы не передавали свое собственное представление в диалоговое окно ... так что я передал его в объект просмотра вкратце новый ViewHolder (dialogView), это волшебное заклинание здесь abra ka dabra boom
и (github.com/orhanobut/dialogplus) эта ссылка мне помогла, так что сначала вы должны прочитать приведенные здесь инструкции git .. удачного кодирования
@GhostCat спасибо я отредактировал свою тему