如何在其中一个键入后设置不可编辑的其他EditTexts?

时间:2018-04-29 11:40:16

标签: java android android-studio if-statement android-edittext

我的App中有四个EditTexts

并且想要在我们写入其中一个后立即编辑所有其他3个edittexts吗?

我怎样才能实现它?

2 个答案:

答案 0 :(得分:2)

您需要在所有编辑文本上添加文本观察器,并且在每个观察者中需要禁用其他编辑文本。 你需要这个观察者中的四个

field1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
    // now check if text is not empty disable field 2, 3 ,4. 
   }

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {

   }
  });

答案 1 :(得分:1)

您可以向TextChangedListener添加EditText,并且每当相应EditText的文字发生更改时,系统都会触发该EditText

您可以通过以下代码使任何editText.setInputType(InputType.TYPE_NULL); 无法编辑:

editText1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
       editText2.setInputType(InputType.TYPE_NULL);
       editText3.setInputType(InputType.TYPE_NULL);
       editText4.setInputType(InputType.TYPE_NULL);
   }

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {

   }
});

所以你的整个代码将是:

EditText

相同的代码将用于剩余的3 //shared.module.ts @NgModule({ imports:[], declarations: [StartComponent], exports: [StartComponent] }) export class SharedModule { } //appmodule.ts @NgModule({ declarations: [ AppComponent, ... ], imports: [ SharedModule, AdminModule, .... ], providers: [ .... ], bootstrap: [AppComponent] }) export class AppModule { } //You can use StartComponent in your ClientListComponent //ClientListComponent.ts import { StartComponent } from '../../../sharedModule/StartComponent.ts; @Component({ selector: 'app-root', template: ` <app-client-list></app-client-list> ` }) s。