【问题标题】:Change the text message, to be a clikable message将短信更改为可点击的消息
【发布时间】:2014-02-27 11:16:12
【问题描述】:
我正在尝试更改 AlertDialog 消息的文本消息。成为可点击的消息。
使用此代码,文本为蓝色,但不可点击
我希望文本中的电话、电子邮件和网站可以点击
final SpannableString All =
new SpannableString(textmess + "\n\nTel :\n " + phone + "\n\nEmail : \n " + email + "\n\nAddress: \n " + address + "\n\nWeb site : \n " + site );
Linkify.addLinks(All, Linkify.ALL );
AlertDialog.Builder builder = new AlertDialog.Builder( new
ContextThemeWrapper(context, R.style.AlertDialogCustom) );
builder.setTitle(title).setMessage(All);
builder.create();
//not working
//TextView textView = (TextView) findViewById(android.R.id.message);
builder.show();
GeoPoint1.close();
db1.close();
【问题讨论】:
标签:
android
android-alertdialog
linkify
【解决方案1】:
为对话框创建一个自定义布局,并使用setMovementMethod 完成它
例如:
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate(R.layout.dialog_custom, null);
TextView dTitle = (TextView) view.findViewById(R.id.textDialogTitle);
TextView dMessage = (TextView) view.findViewById(R.id.textDialogMessage);
dMessage.setMovementMethod(LinkMovementMethod.getInstance());
dTitle.setText("Title");
dMessage.setText(ALL);
dialog.setView(view);
dialog.show();
【解决方案2】:
这个例子会帮助你:
res/layout/dialog.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">
<TextView
android:id="@+id/tv_pc_forDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_pc_dialogTitle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:ems="10"
android:layout_centerHorizontal="true"
android:text="THIS IS A CLICKABLE MESSAGE"
android:singleLine="true"
android:textColor="#FFFFFF" >
</TextView>
<TextView
android:id="@+id/tv_pc_dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:text="SEARCH"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bt_pc_goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_pc_forDialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="GO"
android:textColor="#FFFFFF"
android:textSize="25sp" /> </RelativeLayout>
res/layout/activity_main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:text="Open Dialog" />
</RelativeLayout>
MainActivity.java:
package com.bhavit.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openDialog = (Button) findViewById(R.id.button1);
openDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project
tv.setTypeface(tf); // Set the typeface like this
Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Write here, what do you want to do on button click
}
});
TextView tview = (TextView) dialog.findViewById(R.id.tv_pc_forDialog);
tview.setOnClickListener(new View.OnClickListener() { // set onClickListener to the textview
@Override
public void onClick(View v) {
// Write here, what do you want to do on Message click
Toast.makeText(getApplicationContext(), "You clicked on message", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}
}
我在这里所做的是创建了一个自定义对话框,并在 TextView 上设置了一个 onClickListener,显示为消息。像这样,你可以实现你的目标。