简体   繁体   中英

Phone number link not clickable android

x = (TextView)findViewById(R.id.xLink);
x.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            AlertDialog x = new AlertDialog.Builder(this.this).create();

            x.setMessage(Html.fromHtml(getString(R.string.text)));

            x.setButton(AlertDialog.BUTTON_NEUTRAL, "Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            x.show();

            return false;
        }
    });


  <TextView
    android:id="@+id/xLink"
    android:text="Click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/blue1"
    android:paddingTop="30dip"
    android:paddingRight="10dip"
    android:layout_gravity="right"
    android:textSize="16dip" />

 <string name = "text">at&lt;br /&gt;&lt;a href="tel:18004455667"&gt;1-800-445-5667 lt;/a&gt;.</string>

so basically when you click the link a dialog shows up, with the number in which appears to be a link but when you click, it does nothing. Any reason this might be happening? I already tried changing w/ out ascii characters but did nothing/

I believe the HTML anchor is the problem, especially if you didn't set the resource text with fromHTML() as in: textView.setText(HTML.fromHTML(getString(R.string.text)));

The easiest way to make a phone number clickable is autoLink and straight text:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="phone"
    android:text="My Number: 1-800-762-2035"
     />

Try reading the documentation at:

http://developer.android.com/reference/android/text/util/Linkify.html

You may need to clean up your text as the garbage around it may be causing the problem.

Here is your answer, it's tested.

public class MainActivity extends Activity {

    private AlertDialog dialog;

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

        AlertDialog.Builder builder = new Builder(MainActivity.this);

        Spanned message = Html.fromHtml(getString(R.string.text));
        final SpannableString str = new SpannableString(message);
        Linkify.addLinks(str, Linkify.ALL);

        builder.setMessage(str);

        builder.setPositiveButton("OK", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });

        dialog = builder.create();

        TextView t = (TextView) findViewById(R.id.textView1);
        t.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                dialog.show();

                // This must be called after dialog.show();
                ((TextView)dialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

                return false;

            }
        });


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM