【问题标题】:Select and copy a text from a TextView in a Dialog在对话框中从 TextView 中选择并复制文本
【发布时间】:2020-07-23 13:17:35
【问题描述】:

我在从 textView 中选择文本时遇到问题,每次长按选择文本时,应用程序都会崩溃,我不明白为什么。我认为这主要是因为它在对话框上的文本视图。

我使用以下类:

文本视图:

 <TextView
        style="@style/PUPStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/manual_entry"
        android:enabled="true"
        android:textIsSelectable="true"
        android:focusable="true"
        android:longClickable="true"/>

对话框类:

public class CardReadAlertDialog extends Dialog implements CardReadAlertUseCases {

private LinearLayout mManualInput;
private LinearLayout mReadCard;
private TextView mMessageView;
private Handler mTimer;
private Button btnManualInput;

public CardReadAlertDialog(@NonNull Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.card_read_alert);
    setCancelable(false);

    findViewById(R.id.btn_cancel).setOnClickListener((view) -> abortProcess(true));

    findViewById(R.id.btn_cancel_read).setOnClickListener((view) -> abortProcess(true));

    btnManualInput = findViewById(R.id.btn_manual_input);
    btnManualInput.setOnClickListener((view) -> createManualInput());

    findViewById(R.id.btn_accept).setOnClickListener((view) -> {
        ManualCardEntry manualCardEntry = getManualEntryFromInput();
        State.getInstance().setManualEntry(manualEntry);

        if (manualCardEntry != null) {
            abortProcess(false);
        }
    });


    mManualInput = findViewById(R.id.manual_input_view);
    mReadCard = findViewById(R.id.card_reading_view);
    mMessageView = findViewById(R.id.read_message);
}

错误日志:

09-09 09:01:14.334 16201-16201/com.carslash.carslashcarslash E/AndroidRuntime: 致命异常: main 进程:com.carslash.carslashcarslash,PID:16201 android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:578) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在 android.widget.PopupWindow.invokePopup(PopupWindow.java:1104) 在 android.widget.PopupWindow.showAtLocation(PopupWindow.java:933) 在 com.android.internal.policy.impl.PhoneWindow$DecorView$1.run(PhoneWindow.java:2742) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5271) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

欢迎任何帮助,在此先感谢您。

【问题讨论】:

  • 发布崩溃错误日志
  • 好的,刚刚发布了错误日志。
  • 看到你的错误在于 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);阅读此stackoverflow.com/questions/30502191/…
  • 感谢它帮助我解决了我的问题。

标签: java android textview dialog


【解决方案1】:

您可以将 EditText 与 textviewstyle 一起使用,它应该可以工作

<EditText android:id="@+id/showText"
    style="?android:attr/textViewStyle"
    android:background="@null"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

参考https://stackoverflow.com/a/7198401/12676247

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2011-08-26
    • 1970-01-01
    • 2011-06-26
    • 2015-07-05
    • 2019-01-22
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多