Last active
August 15, 2020 07:08
-
-
Save neiraza/6118527 to your computer and use it in GitHub Desktop.
複数のEditTextにたいして、TextWatcherをしかけてみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by togu on 2013/07/23. | |
*/ | |
public class HogeActivity extends Activity { | |
EditText editText1; | |
EditText editText2; | |
Button bomb; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); | |
setContentView(R.layout.fuga); | |
editText1 = (EditText) findViewById(R.id.edit_text1); | |
editText1.addTextChangedListener(new GenericTextWatcher(editText1)); | |
editText2 = (EditText) findViewById(R.id.edit_text2); | |
editText2.addTextChangedListener(new GenericTextWatcher(editText2)); | |
//簡単には押せないのだ | |
bomb = (Button) findViewById(R.id.bomb); | |
bomb.setEnabled(false); | |
} | |
private class GenericTextWatcher implements TextWatcher { | |
private View view; | |
private GenericTextWatcher(View view) { | |
this.view = view; | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
switch (view.getId()) { | |
case R.id.edit_text1: | |
//入力してくれないと、このボタン押せないんだからね | |
if (s.length() > 0) { | |
bomb.setEnabled(true); | |
} else { | |
next.setEnabled(false); | |
} | |
break; | |
case R.id.edit_text2: | |
//別にif文じゃなくてもよいし、入力文字数の条件をかえてもよいし、 | |
//ボタンタップ条件を他のEditTextとからめても良いよ | |
break; | |
} | |
} | |
public void afterTextChanged(Editable editable) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment