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

首頁 > 學院 > 開發設計 > 正文

EditText的常用屬性和實例

2019-11-09 14:57:47
字體:
來源:轉載
供稿:網友
EditText的常用屬性android:text 設置顯示在EditText中的內容android:maxLength 用來限制EditText中可以輸入的字符個數android:hint 設置顯示在EditText上的提示信息android:numeric 設置編輯框中只能輸入數字android:passWord 設置EditText以輸入密碼模式來顯示android:lines 設置EditText中可以輸入的字符行數Android:digits 設置只能輸入的內容給EditText添加監聽: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 s) {}EditText實例1:在EditText中最多只能輸入30個字符,當到第30個字符時提示"文字不能超過30"首先創建布局文件。代碼:<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="發表"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("文字不能超過30");}else{mTextView2.setText("");}}});}}EditText實例2:EditText中可以輸入多個字符,但是當超過30時,發表按鈕不能點擊布局中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){//按鈕處于不可點擊狀態btn.setEnabled(false);mTextView2.setText("文字不能超過30");}else{//按鈕處于可點擊狀態btn.setEnabled(true);mTextView2.setText("");}}});}}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滕州市| 新闻| 华安县| 班玛县| 兴安盟| 台北市| 和田市| 洮南市| 长宁区| 和龙市| 界首市| 贺州市| 陕西省| 连州市| 肇东市| 巩义市| 凤台县| 商南县| 政和县| 丽江市| 吴旗县| 武汉市| 北流市| 河南省| 景东| 长葛市| 交城县| 泽库县| 喀喇沁旗| 章丘市| 雷波县| 子长县| 松溪县| 浑源县| 和顺县| 甘肃省| 阿拉尔市| 安远县| 泸州市| 开化县| 东宁县|