从对话框中的 TextView 中选择并复制文本。

huangapple 未分类评论44阅读模式
英文:

Select and copy a text from a TextView in a Dialog

问题

我在选择textView中的文本时遇到了问题,每次我长按以选择文本时,应用程序都会崩溃,我不明白原因。我认为主要是因为该文本视图位于对话框上。

我使用以下类:

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"/>

Dialog类:

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: FATAL EXCEPTION: main
Process: com.carslash.carslashcarslash, PID: 16201
android.view.WindowManager$BadTokenException: Unable to add window -- token 
null is not for an application
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:578)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
    at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104)
    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:933)
    at com.android.internal.policy.impl.PhoneWindow$DecorView$1.run(PhoneWindow.java:2742)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5271)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

欢迎提供任何帮助,提前谢谢。

英文:

I'm having trouble selecting a text from a textView, every time I long press to select a text the application crashes and I don't understand why. I think it's mainly because the text view it's on a Dialog.

I use the following classes:

TextView:

 &lt;TextView
        style=&quot;@style/PUPStyle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/manual_entry&quot;
        android:enabled=&quot;true&quot;
        android:textIsSelectable=&quot;true&quot;
        android:focusable=&quot;true&quot;
        android:longClickable=&quot;true&quot;/&gt;

Dialog class:

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) -&gt; abortProcess(true));

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

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

    findViewById(R.id.btn_accept).setOnClickListener((view) -&gt; {
        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);
}

Error log:

> 09-09 09:01:14.334 16201-16201/com.carslash.carslashcarslash
> E/AndroidRuntime: FATAL EXCEPTION: main
> Process: com.carslash.carslashcarslash, PID: 16201
> android.view.WindowManager$BadTokenException: Unable to add window -- token
> null is not for an application
> at android.view.ViewRootImpl.setView(ViewRootImpl.java:578)
> at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
> at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
> at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104)
> at android.widget.PopupWindow.showAtLocation(PopupWindow.java:933)
> at com.android.internal.policy.impl.PhoneWindow$DecorView$1.run(PhoneWindow.java:2742)
> at android.os.Handler.handleCallback(Handler.java:739)
> at android.os.Handler.dispatchMessage(Handler.java:95)
> at android.os.Looper.loop(Looper.java:135)
> at android.app.ActivityThread.main(ActivityThread.java:5271)
> at java.lang.reflect.Method.invoke(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:372)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

Any help is welcome, thank you beforehand.

答案1

得分: 0

你可以使用带有textviewstyle的EditText,它应该可以工作。

<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

英文:

You can use and EditText with textviewstyle it should work

&lt;EditText android:id=&quot;@+id/showText&quot;
    style=&quot;?android:attr/textViewStyle&quot;
    android:background=&quot;@null&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;wrap_content&quot;/&gt;

Reference https://stackoverflow.com/a/7198401/12676247

huangapple
  • 本文由 发表于 2020年7月23日 21:17:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/63055245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定