簡體   English   中英

TextView performClick() 不點擊鏈接

[英]TextView performClick() doesn't click a link

我有一個帶有android:autoLink="all"TextView

<TextView
    android:id="@+id/text"
    android:text="abcdefgh@def.com"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="all" />

出於某種原因,我想打開一個在android:text設置的鏈接(可能是電話號碼、電子郵件等)。 當我運行應用程序時, performClick()不會打開鏈接。

TextView textView = findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.performClick();

Linkify.addLinks(buffer, Linkify.ALL); 而其他一些則無濟於事。

更新

感謝您的回復。 收到 3 個答案后顯示我的代碼。 我使用 API 19、25 模擬器和設備 (API 21)。 可悲的是,沒有任何作用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:text="abcdefgh@def.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:autoLink="web|email|phone" />

</android.support.constraint.ConstraintLayout>

主要活動:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = findViewById(R.id.text);
        int mask = Linkify.ALL;
        Linkify.addLinks(text, mask);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.performClick();
    }
}

只需在您的 xml 中使用此android:autoLink="web" 它對我有用。 讓我知道它是否有效

使用autoLink="web"

<TextView
    android:id="@+id/text"
    android:text="abcdefgh@def.com"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web" />

要以編程方式執行此操作,請使用view.setMovementMethod(LinkMovementMethod.getInstance()); 在您的代碼中,並確保您的 XML 布局中沒有android:autoLink="web"

示例:

TextView text = (TextView) findViewById(R.id.text);
    text.setMovementMethod(LinkMovementMethod.getInstance());

編輯

也使用android:linksClickable="true" 它可能會起作用。 還要確保使用autoLinksetMovementMethod 不要同時使用它們。

編輯 2

我意識到您想在performClick()上使用performClick() 盡管android.widget.TextView.performClick()例子並不多,這意味着該方法要么不受歡迎,要么過時。 但是它可以像這樣實現:

TextView text ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView)findViewById(R.id.Text1);
    text.setOnClickListener(this);
}

public void onClick(View arg0) {
      Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
        startActivity(i);
    }
}

這符合您的要求。 檢查它是否有效。

我們需要覆蓋 performClick():

@Override
 public boolean performClick() {
  // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
 }

不幸的是,您試圖實現的目標是不可能的。 基於 textView.performClick() 的文檔:

調用此視圖的 OnClickListener(如果已定義)

由於您尚未定義任何 onClickListener,這些方法將不起作用。

解決方法是定義您的自定義 onClickListener 並嘗試以編程方式解析 URI 以觸發適當的Intent.ACTION_VIEW

暫無
暫無

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

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