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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

EditText的常用屬性和實(shí)例

2019-11-09 14:04:00
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
EditText的常用屬性android:text 設(shè)置顯示在EditText中的內(nèi)容android:maxLength 用來(lái)限制EditText中可以輸入的字符個(gè)數(shù)android:hint 設(shè)置顯示在EditText上的提示信息android:numeric 設(shè)置編輯框中只能輸入數(shù)字android:passWord 設(shè)置EditText以輸入密碼模式來(lái)顯示android:lines 設(shè)置EditText中可以輸入的字符行數(shù)Android:digits 設(shè)置只能輸入的內(nèi)容給EditText添加監(jiān)聽(tīng):mEditText.addTextChangedListener(new TextWatcher() {@Override//在文本改變時(shí),該方法被第一個(gè)調(diào)用public void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Override//當(dāng)文本正在改變時(shí)被調(diào)用public void onTextChanged(CharSequence s, int start, int before, int count) {}@Override//當(dāng)文本改變后被調(diào)用public void afterTextChanged(Editable s) {}EditText實(shí)例1:在EditText中最多只能輸入30個(gè)字符,當(dāng)?shù)降?0個(gè)字符時(shí)提示"文字不能超過(guò)30"首先創(chuàng)建布局文件。代碼:<RelativeLayoutxmlns: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="com.zhiyuan3g.edittextlx.Main2Activity"><EditTextandroid:maxLength="30"android:layout_width="wrap_content"android:layout_height="100sp"android:id="@+id/editText2"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_alignParentEnd="true"/><TextViewandroid:textSize="35sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0/30"android:id="@+id/textView"android:layout_below="@+id/editText2"android:layout_alignParentStart="true"/><Buttonandroid:textSize="20sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="發(fā)表"android:id="@+id/button"android:layout_below="@+id/editText2"android:layout_alignParentEnd="true"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textView2"android:layout_below="@+id/button"android:layout_alignParentStart="true"/></RelativeLayout>在java文件中:public class Main2Activity extends AppCompatActivity {PRivate Button btn;private EditText mEditText;private TextView mTextView,mTextView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);initView();}private void initView() {btn = (Button) findViewById(R.id.button);mEditText = (EditText) findViewById(R.id.editText2);mTextView = (TextView) findViewById(R.id.textView);mTextView2 = (TextView) findViewById(R.id.textView2);mEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {mTextView.setText(s.toString().length()+"/30");if(s.toString().length()==30){mTextView2.setText("文字不能超過(guò)30");}else{mTextView2.setText("");}}});}}EditText實(shí)例2:EditText中可以輸入多個(gè)字符,但是當(dāng)超過(guò)30時(shí),發(fā)表按鈕不能點(diǎn)擊布局中EditText的代碼:<EditTextandroid:layout_width="wrap_content"android:layout_height="100sp"android:id="@+id/editText2"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_alignParentEnd="true"/>在java文件中的代碼:public class Main2Activity extends AppCompatActivity {private Button btn;private EditText mEditText;private TextView mTextView,mTextView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);initView();}private void initView() {btn = (Button) findViewById(R.id.button);mEditText = (EditText) findViewById(R.id.editText2);mTextView = (TextView) findViewById(R.id.textView);mTextView2 = (TextView) findViewById(R.id.textView2);mEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {mTextView.setText(s.toString().length()+"/30");if(s.toString().length()>30){//按鈕處于不可點(diǎn)擊狀態(tài)btn.setEnabled(false);mTextView2.setText("文字不能超過(guò)30");}else{//按鈕處于可點(diǎn)擊狀態(tài)btn.setEnabled(true);mTextView2.setText("");}}});}}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 双流县| 汉寿县| 镇雄县| 红河县| 台湾省| 谢通门县| 商河县| 德化县| 信阳市| 麻城市| 丽水市| 东乌珠穆沁旗| 青冈县| 原平市| 金塔县| 弥渡县| 刚察县| 壤塘县| 兴宁市| 静海县| 德江县| 安阳市| 仲巴县| 阿拉善右旗| 邵武市| 松滋市| 宁南县| 博野县| 泗洪县| 云梦县| 商水县| 周口市| 阜南县| 奉化市| 广德县| 浙江省| 武宣县| 崇明县| 新疆| 焦作市| 德州市|