国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 淳安县| 抚远县| 原平市| 黄石市| 红原县| 博野县| 绥芬河市| 古丈县| 永福县| 江都市| 镇雄县| 抚松县| 水富县| 阿拉尔市| 舞钢市| 色达县| 澜沧| 莱州市| 海口市| 和平区| 阳高县| 婺源县| 醴陵市| 宁德市| 乌拉特后旗| 化州市| 西城区| 三亚市| 积石山| 讷河市| 镇远县| 庄河市| 全南县| 钟祥市| 富蕴县| 怀安县| 北京市| 沅陵县| 济源市| 嘉荫县| 水城县|