【问题标题】:How can I make hyperlinks in popup windows clickable in Android Studio?如何在 Android Studio 中点击弹出窗口中的超链接?
【发布时间】:2022-01-08 12:34:20
【问题描述】:

我有一个 Android 应用程序,它会打开一些弹出窗口。但弹出窗口中的超链接不可点击。我尝试了一些方法,例如: 在activity_main.xml中

    <TextView
`.....`
`android:autoLink="web"`
`android:autoLink="all"`

我认为问题在于我的文本在弹出窗口中。 代码如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView dataList = findViewById(R.id.dataList);

        dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BlackBoardEntry entry = (BlackBoardEntry) parent.getItemAtPosition(position);
                String content = entry.getContent();
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View popupView = inflater.inflate(R.layout.content_popup, null, false);
                ((TextView) popupView.findViewById(R.id.contentView)).setText(content);

                PopupWindow popup = new PopupWindow(popupView, 500, 1000, true);
                popup.showAtLocation(parent, Gravity.CENTER, 0, 0);


                }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<BlackBoardEntry> entries = BlackBoardClient.getBlackBoardEntries();
                    BlackBoardAdapter adapter = new BlackBoardAdapter(entries, getApplicationContext());
                    dataList.post(new Runnable() {
                        @Override
                        public void run() {
                            dataList.setAdapter(adapter);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

看起来像这样:

【问题讨论】:

    标签: java android android-studio popupwindow


    【解决方案1】:
       public static void createClickableSpanText(ClickableSpan listener, TextView textView, String message, int startIndex, int endIndex) {
            SpannableString spannableString = new SpannableString(message);
            spannableString.setSpan(listener, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    

    您可以执行类似上述操作来为 startIndex 和 endIndex 之间的文本设置“可点击范围”。这应该允许您单击该特定文本。

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多