簡體   English   中英

TextView 沒有在里面設置文本包括布局

[英]TextView doesn't setText inside include layout

我有一個自定義對話框,其中包含另一個布局。 在此布局中,有 3 個按鈕和一個TextView ,這些都不是 null。 如果我單擊按鈕,它們會正常工作。 但是 TextView 不顯示任何文本。 當我單擊另一個按鈕時,應該會出現文本。 這就是它應該如何工作

自定義對話框布局:

<LinearLayout 
   ............

.....
<androidx.cardview.widget.CardView
        android:id="@+id/result_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="2dp"
        card_view:cardMaxElevation="2dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:visibility="gone"
        app:cardCornerRadius="8dp">

    <include android:id="@+id/result_layout" layout="@layout/result_block" />

    </androidx.cardview.widget.CardView>

....
....
</LinearLayout>

result_block布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:id="@+id/result_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:text=""
        android:textColor="@color/colorWhite"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal">
        <ImageButton
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_copy_24px_outlined"
            android:layout_marginEnd="16dp"
            android:tint="@color/colorWhite"
            android:background="?attr/selectableItemBackgroundBorderless"
            />

        <ImageButton
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:tint="@color/colorWhite"
            android:src="@drawable/ic_share_24px_outlined"
            android:background="?attr/selectableItemBackgroundBorderless"
            />

        <ImageButton
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@color/colorWhite"
            android:src="@drawable/ic_volume_up_24px_outlined"
            android:background="?attr/selectableItemBackgroundBorderless"
            />
    </LinearLayout>

</LinearLayout>

現在對話框

private void showDiag() {
        final View dialogView = View.inflate(getActivity(), R.layout.custom_dialog_layout,null);
        final View resultLayout = View.inflate(getActivity(), R.layout.result_block,null);
        final Dialog dialog = new Dialog(getActivity(), R.style.MyAlertDialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(dialogView);

        final TextView resultTxt = resultLayout.findViewById(R.id.result_txt);
        final ImageButton btn1 = resultLayout.findViewById(R.id.btn1);
        final CardView resultCardView = dialog.findViewById(R.id.result_card_view);
        final TextInputEditText editText = dialog.findViewById(R.id.text_edit);
        final ImageButton clearText = dialog.findViewById(R.id.clear_text);
        MaterialButton resultConfirm = dialog.findViewById(R.id.result_confirm);
        ImageButton btn2 = dialogView.findViewById(R.id.copy_btn);
        ImageButton btn3 = dialogView.findViewById(R.id.share_btn);

        resultConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!editText.getText().toString().equals("")) {
                    String theWord = editText.getText().toString();
                    String result = Utils.getSecondWord(theWord);
                    // result it shows me the correct string with no errors or something else
                    resultTxt.setText(result); // doesn't work. Not set the text
                    insertWord(theWord, result);
                    resultCardView.setVisibility(View.VISIBLE);
                    resultCardView.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                }
            }
        });

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        // Share btn
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.i(TAG, "result: " + resultTxt.getText().toString());
            }
        });

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Utils.revealShow(dialogView, true, null, resultConfirm);
            }
        });

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.KEYCODE_BACK){
                    Utils.revealShow(dialogView, false, dialog, resultConfirm);
                    return true;
                }
                return false;
            }
        });
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.show();
    }

對話框中的所有內容都可以正常工作,但 TextView 不能。 即使我嘗試寫一些其他的東西,比如resultTxt.setText("Hello there"); 文本不出現。 有什么建議么?

Please you remove the android:text="" in TextView because textview is get default text is empty or you write anything in TextView like android:text="abc"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM