国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 開發中根據搜索內容實現TextView中的文字部分加粗

2019-10-23 18:33:36
字體:
來源:轉載
供稿:網友

在生產型Android客戶端軟件(企業級應用)開發中,界面可能存在多個輸入(EditText)和多個操作(MotionEvent和KeyEvent),且操作依賴于輸入的狀態。如下圖所示的場景:

android,textview加粗,textview,文字加粗

設定圖中

  • 確認操作依賴于商品編碼和儲位的狀態
  • 跳過操作不依賴于輸入狀態
  • 登記差異操作依賴于儲位和數量的狀態

輸入框有三種狀態:

  • 待輸入;
  • 待校驗;
  • 校驗成功。

操作需要當其依賴的輸入數據校驗成功,才能執行。

如果在Activity中去判斷輸入框狀態,那么實際需要調用(3個輸入框)*(3種狀態)*(3個按鈕) = 27個 if 判斷,對于狀態的維護將使得整個程序可維護性極差,并隨著輸入和操作的增加,維護的狀態呈指數增長。

通過對這種場景的抽象,實現了Android控件狀態依賴框架,其使用方法如下:

使用方法:

1、布局文件引用WatchEditText和WatchButton

<com.android.yhthu.viewdependency.view.WatchEditTextandroid:id="@+id/edit_query_1"android:layout_width="match_parent"android:layout_height="wrap_content"android:tag="editQuery1"android:imeOptions="actionNext"android:hint="商品編碼"android:inputType="number"/><com.android.yhthu.viewdependency.view.WatchButtonandroid:id="@+id/search_button_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:tag="buttonSearch1"android:text="確認" />

由于Library Module中的控件id不是常量(可參考ButterKnife對Library Module的支持采用R2的原因),這里采用了tag的方式。

2、在Activity中通過注解申明依賴

@ViewName("商品編碼")private WatchEditText editQuery1;@ViewName("儲位")private WatchEditText editQuery2;@ViewName("數量")private WatchEditText editQuery3;@ViewDependency(name = @ViewName("確認"), dependency = {"editQuery1", "editQuery2"})private WatchButton buttonSearch1;@ViewDependency(name = @ViewName("跳過")/*不依賴輸入*/)private WatchButton buttonSearch2;@ViewDependency(name = @ViewName("登記缺貨"), dependency = {"editQuery2", "editQuery3"})private WatchButton buttonSearch3;

ViewName定義控件名稱,ViewDependency中dependency指定其依賴的控件tag。

3、直接執行onClick和onEditorAction(修改狀態)

@Overridepublic void onClick(View v) {if (v == buttonSearch1) { Toast.makeText(this, "調接口", Toast.LENGTH_SHORT).show();} else if (v == buttonSearch2) { Toast.makeText(this, "跳下一頁", Toast.LENGTH_SHORT).show();} else if (v == buttonSearch3) { Toast.makeText(this, "登記缺貨", Toast.LENGTH_SHORT).show();}}

可以看出,這里并沒有通過if判斷各個輸入控件的狀態。

@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {if (actionId == EditorInfo.IME_ACTION_NEXT && v == editQuery1 && (query1Str = editQuery1.getText().toString()).isEmpty()) { if (query1Str.equals("12345")) { editQuery1.complete(); return true; }} // 省略代碼return false;}

onEditorAction模擬調用軟件的Enter進行校驗,這里需要注意通過editQuery1.complete()修改該EidtText的狀態。

實現原理

整個框架分為三個package:annotation、state和view。

1、在annotation中定義ViewName和ViewDependency注解,分別用于WatchEditText和WatchButton。ViewName指定WatchEditText控件在業務中的名稱,ViewDependency指定WatchButton依賴的WatchEditText控件;

@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface ViewDependency {ViewName name() default @ViewName;String[] dependency() default {};}

2、在state中通過狀態模式定義Enter、Verify、Complete,其基類為抽象類Operator,定義方法operator;

public abstract class Operator {// 操作對應的上下文protected Context context;// 操作public abstract boolean operator(String operatorName, String viewName);}
public class Enter extends Operator {private static Enter enter;private Enter(Context context) { this.context = context;}public static Enter getInstance(Context context) { if (enter == null) { enter = new Enter(context); } return enter;}@Overridepublic boolean operator(String operatorName, String viewName) { Toast.makeText(context, String.format("[%s]為空,不允許執行[%s]", viewName, operatorName),  Toast.LENGTH_SHORT).show(); return false;}}

3、WatchEditText和WatchButton定義控件的依賴關系。WatchEditText實現ViewState接口,其包含三種狀態的轉換方法。

public interface ViewState {void enter();void verify();void complete();}

以上,博客園對markdown支持的不太好,無法添加注釋(/* */),如需查看源碼,請移步Github地址:https://github.com/yhthu/AndroidViewDependency.git

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望

實現方式沒有引入任何依賴,輕量級實現需求效果

最近遇到一個需求,需要做一個搜索功能。搜索的內容需要加粗顯示。

完成了這個功能后,寫下此博客,記錄一下實現過程

效果圖

android,textview加粗,textview,文字加粗

首先自定義一個StyleSpan,在StyleSpan里做加粗的等匹配狀態的設置

@SuppressLint("ParcelCreator")public class SearchStyleSpan extends StyleSpan { public SearchStyleSpan(int style) {  super(style); } @Override public void updateDrawState(TextPaint ds) {  ds.setFakeBoldText(true);  //FIXME 這里還可以做其他差異性設置(修改文字大小等)  super.updateDrawState(ds); } @Override public void updateMeasureState(TextPaint paint) {  paint.setFakeBoldText(true);  super.updateMeasureState(paint); }}

監聽編輯框的內容變化

mEditText.addTextChangedListener(new TextWatcher() {   @Override   public void beforeTextChanged(CharSequence s, int start, int count, int after) {   }   @Override   public void onTextChanged(CharSequence s, int start, int before, int count) {   }   @Override   public void afterTextChanged(Editable editable) {    checkSearchContent(mEditText.getText().toString());   }  });

通過循環,將TextView 里的文字根據匹配內容分段

SpannableStringBuilder searchStyle = new SpannableStringBuilder();  int start;  while (content.contains(searchContent)) {   start = content.indexOf(searchContent);   searchStyle.append(getBoldSpannable(content.substring(0, start + searchContent.length()), searchContent));   content = content.substring(start + searchContent.length());  }  searchStyle.append(content);

將分段好的文字進行加粗處理

 int start = content.indexOf(searchContent);  SpannableStringBuilder ssb = new SpannableStringBuilder(content);  ssb.setSpan(new SearchStyleSpan(Typeface.NORMAL), start, start + searchContent.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

源碼地址:https://github.com/TitleZWC/BoldSpannable

以上所述是小編給大家介紹的Android 開發中根據搜索內容實現TextView中的文字部分加粗,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!

大家多多支持VEVB武林網。

 


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱芜市| 丰县| 毕节市| 厦门市| 商河县| 东港市| 寿光市| 宁南县| 通榆县| 莆田市| 抚州市| 琼海市| 武乡县| 钟山县| 灵璧县| 会理县| 化德县| 哈密市| 关岭| 垦利县| 锦州市| 大港区| 象州县| 个旧市| 鄂托克旗| 武强县| 藁城市| 富裕县| 伊通| 磴口县| 堆龙德庆县| 松桃| 沅陵县| 渝中区| 巴青县| 象山县| 临洮县| 高阳县| 双辽市| 五寨县| 禄劝|