2016-05-17 159 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(); 
相關問題