


首先創(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("");}}});}}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注