2016-05-17 156 views
-1

我正在开发一个应用程序,我使用材料设计对话框向用户显示一些信息,我想要的是在对话框中使文本样式变为粗体。如何实现此操作问题。如何在字符串中加粗和颜色特定文本

这里是我的对话框的图片: - enter image description here

在这里我想这个手机号码的文本样式大胆的色彩

这里是我的代码: -

AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this); 
      builder.setCancelable(false); 
      builder.setMessage("We will send you a verification SMS to this Number.\n\n"+s_szResponseMobileNum+"\n\nIs this OK,or would you like to edit the number."); 
      builder.setPositiveButton(R.string.ok, null); 
      builder.setNegativeButton(R.string.edit,null); 
      builder.show(); 
+0

作出this.it定制对话框很容易定制你的文本样式和颜色 –

回答

2

对于特定风格文字我们要将文字转换为HTML并使用HTML标签对于造型

尝试,这可能是它的工作....................

String sourceString = "We will send you a verification SMS to this Number.<br><br> <b><font color='#08b608'>" + s_szResponseMobileNum + "</b> <br><br>Is this OK,or would you like to edit the number." ; 
builder.setMessage(Html.fromHtml(sourceString)); 

与输出: - Image

享受编码.....................

+0

我THIK你不明白我的问题 – Nitin

+0

我想上面款式和颜色只有你的意思是你要在不同的风格消息 – Nitin

+0

移动提及。 .....? – sushildlh

0

你基本上可以使用HTML标签在你的字符串资源,如:

<resource> 
    <string id="@+id/no">We will send you a verification SMS to this Number <b>your mobile no</b>this OK,or would you like to edit the number</string> 
</resources> 
+0

但手机号码是我从服务器响应获取并保存在名为“s_szResponseMobileNum”的varibale中,然后我如何在字符串xml中定义 – Nitin

+0

@Nitin自定义对话框,以便您可以更改任何文本样式或颜色 –

+0

@Nitin试试这个参考:http://www.mkyong.com/android/android-custom-dialog-example/否则发送您的邮件ID我发送您的演示 –

0

我得到安宁g您一步步引导

STEP 1

添加支持的lib在你的gradle这个文件

dependencies { 
    compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version 
} 

STEP 2

让您的活动延长android.support.v7.app.AppCompatActivity.

public class MainActivity extends AppCompatActivity { 
     ... 
    } 

STEP 3创建警报对话框

private void showLocationDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
    builder.setTitle(getString(R.string.dialog_title)); 
builder.setMessage(Html.fromHtml(""+"<b>"+R.string.dialog_message+"</b>")); 

String positiveText = getString(android.R.string.ok); 
    builder.setPositiveButton(positiveText, 
      new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // positive button logic 
     } 
    }); 

    String negativeText = getString(android.R.string.cancel); 
    builder.setNegativeButton(negativeText, 
      new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // negative button logic 
     } 
    }); 

    AlertDialog dialog = builder.create(); 
    // display dialog 
    dialog.show(); 
} 

STEP 4

申报对话背景定制drawable.xml。在style.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- From: support/v7/appcompat/res/drawable/abc_dialog_material_background_light.xml --> 
<inset xmlns:android="http://schemas.android.com/apk/res/android" 
    android:insetLeft="16dp" 
    android:insetTop="16dp" 
    android:insetRight="16dp" 
    android:insetBottom="16dp"> 

    <shape android:shape="rectangle"> 
     <corners android:radius="2dp" /> 
     <solid android:color="@color/indigo" /> 
    </shape> 

</inset> 

申报风格文件

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <!--buttons color--> 
    <item name="colorAccent">@color/pink</item> 
    <!--title and message color--> 
    <item name="android:textColorPrimary">@android:color/white</item> 
    <!--dialog background--> 
    <item name="android:windowBackground">@drawable/background_dialog</item> 
</style> 

STEP 5最后一步

创建您的对话框,并使用风格作为AlertDialog.Builder参数。

AlertDialog.Builder builder = 
     new AlertDialog.Builder(this, R.style.MyDialogTheme); 
... 
AlertDialog dialog = builder.create(); 
// display dialog 
dialog.show(); 
0

您可以通过下面的代码解决您的查询,这将帮助您以粗体显示带有特定颜色的数字。

您可以使用Spannable类来尝试下面的代码。

 String strMessage = “We will send you a verification SMS to this Number.\n\n” + s_szResponseMobileNum + "\n\nIs this OK,or would you like to edit the number." 

     Spannable wordtoSpan = new SpannableString(strMessage);   

     wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), strMessage.indexOf(s_szResponseMobileNum), s_szResponseMobileNum.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     wordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), strMessage.indexOf(s_szResponseMobileNum), s_szResponseMobileNum.length, 0); 

     AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this); 
     builder.setCancelable(false); 
     builder.setMessage(wordtoSpan); 
     builder.setPositiveButton(R.string.ok, null); 
     builder.setNegativeButton(R.string.edit,null); 
     builder.show(); 

希望这会起作用。

0

您可以使用StyleSpan使文本的特定部分变为粗体。

SpannableStringBuilder ssb = new SpannableStringBuilder(); 
ssb.append("We will send you a verification SMS to this Number:\n\n"); 
int start = ssb.length(); 
ssb.append(phoneNumber); 
ssb.append("\n\nIs this OK, or would you like to edit the number?"); 
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, phoneNumber.length(), 
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

AlertDialog.Builder builder = new AlertDialog.Builder(CRegistrationScreen.this); 
builder.setCancelable(false) 
     .setMessage(ssb) 
     .setPositiveButton(R.string.ok, null) 
     .setNegativeButton(R.string.edit, null) 
     .show(); 
相关问题