執行時建立用於輸入密碼的 TextView

2021-08-31 04:31:46 字數 945 閱讀 2465

某些場合,可能需要在執行時令某個 textview (可能是執行時建立的,也可以是寫在 xml 檔案中的)。由於無法通過 xml 檔案指定其為 password 輸入屬性,那麼如何實現這個效果呢?

textview 有兩個方法:

setinputtype(int)

settransformationmethod(transformationmethod)

其中 setinputtype 可以更改 textview 的輸入方式:contact、email、date、time、short message、normal text、password 等。還可以指定各種更正選項,如 單詞首字母大寫、句子首字母大寫、自動更正等。

使用方法:

int inputtype = inputtype.type_class_text | inputtype.type_text_flag_auto_correct

| inputtype.type_text_flag_multi_line | inputtype.type_text_flag_auto_correct

| inputtype.type_text_variation_short_message;

textview.setinputtype(inputtype);

而 settransformationmethod 則可以支援將輸入的字元轉換,包括清除換行符、轉換為掩碼。使用方法:

textview.settransformationmethod(passwordtransformationmethod.getinstance());

綜合來說,如果需要實現自己的轉換,可以通過實現 transformationmethod 介面來達到你的目的(比如讓輸入的所有字元都變成 a,或者輸入 a 顯示 z,輸入 z 顯示 a 等)。

執行時建立類

a.使用objc allocateclasspair建立乙個類class const char classname calculator class kclass objc getclass classname if kclass b.使用class addivar新增乙個成員變數 nsuinteg...

Stopwatch 類用於計算程式執行時間

命名空間 system.diagnostics.stopwatch 例項化 stopwatch gettime new stopwatch 開始計時 gettime.start gettime.stop console.writeline gettime totletime elapsedmilli...

OC執行時動態建立類

it168技術 在前文 深入淺出cocoa之類與物件 一文中,我已經詳細介紹了objc中的 class 與 object 的概念,今天我們來如何在執行時動態建立類。下面這個函式就是應用前面講到的class,metaclass的概念,在執行時動態建立乙個類。這個函式來自 inside mac os x...