【问题标题】:Hyperlinking the Phone number in AlertDialog of Android在 Android 的 AlertDialog 中超链接电话号码
【发布时间】:2011-12-20 11:33:15
【问题描述】:

我有一个 AlertDialog,其文本为 “有关更多信息,请致电 1-800-200-1000”

这是我在单击 ListView 项时显示警报对话框的代码:

 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final SpannableString s = new SpannableString("1-800-200-1000");
        Linkify.addLinks(s, Linkify.ALL);
        ListView.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {

          builder.setMessage("For more information, Please Call "+s)
           .setCancelable(true)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                 dialog.dismiss();
               }
           });
          AlertDialog alert = builder.create();
          alert.show();
        }});

这里我想超链接“1-800-200-1000”,当点击它时,应该实现另一个调用功能的对话框。

这是我的调用函数的 AlertDialog:

    final Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to Call?");
    builder.setCancelable(false);
    builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
       Intent dial = new Intent();
            dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
                                        startActivity(dial); 

                                    }
                                });
                                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                       // Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
                                        dialog.cancel();
                                    }
                                });
                                AlertDialog dialog = builder.create();
                                dialog.show();

请帮我解决两个问题:

1.如何在第一个对话框中超链接电话号码?

2.点击超链接电话号码时如何嵌入Call AlertDialog?

提前致谢。

【问题讨论】:

标签: java android hyperlink android-alertdialog


【解决方案1】:

试试这个:

Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
    //override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

【讨论】:

  • 是的,你也可以使用 Linkify。
猜你喜欢
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 2021-01-25
  • 1970-01-01
相关资源
最近更新 更多