所以我的想法是在EditText中監聽一個數字,例1。當用戶在EditText中編號爲1時,我想讓我的應用程序執行某些操作,但我不知道如何將afterTextChangeListener與if結合起來。如何與afterTextChangeListener一起使用?
1
A
回答
0
您可以使用下面的代碼:
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
// you can write any condition or logic here.
if(editText1.getText().toString.equals("1"){
// You can apply your logic here
}
}
});
0
public class YourClass extends Activity {
private EditText yourEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(yourEditText.getText().toString().equals("1")) {
//Rest of the code goes here
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}
}
0
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
// you can write any condition or logic here.
String textEntered = editText1.getText().toString();
if(isNumeric(textEntered)){
// You can apply your logic here
}
}
});
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(.\\d+)?");
}
相關問題
- 1. 如何使PdfLayer.SetPrint與PdfStamper一起使用?
- 2. 如何使wp_logout_url與wp_nav_menu一起使用?
- 3. 如何將TTTAttributedLabel與'@'和'#'一起使用?
- 4. zeromq如何與SSL一起使用?
- 5. 如何將SystemBarTint與PreferenceFragment一起使用
- 6. 如何將Ninject2與WCF一起使用?
- 7. 如何與Tkinter一起使用PIL?
- 8. 如何將RestSharp與OAuth一起使用?
- 9. 如何將C#與AIR一起使用?
- 10. PersistentVolume如何與hostPath一起使用?
- 11. Bitblt()如何與SRCAND一起使用?
- 12. 如何與groupby一起使用ols?
- 13. 如何將SQLAlchemy與buildozer一起使用?
- 14. 如何將stubsPerConsumer與restdocs一起使用
- 15. Silverlight RenderTransformOrigin如何與TranslateTransform一起使用?
- 16. 如何將ZINCRBY與predis一起使用
- 17. 如何與tidyParseBuffer()一起使用libtidy?
- 18. 如何將selectAll與checkedListBox一起使用..?
- 19. Enum.map如何與Task.await一起使用?
- 20. 如何將yaml.load_all與fileinput.input一起使用?
- 21. 如何將$ _GET與paginator一起使用?
- 22. 如何將socketRocket與socket.io一起使用?
- 23. 如何與Netbeans 8.0.1一起使用SASS
- 24. 如何與Django一起使用PHP?
- 25. FallbackValue如何與MultiBinding一起使用?
- 26. corefx如何與HttpMessageHandler一起使用?
- 27. 如何將LINQ與ServiceModel.Channels.Message一起使用
- 28. 如何將PixelFormats.IndexedX與RenderTargetBitmap一起使用?
- 29. 如何與clang一起使用buildtool?
- 30. 如何與vb.net一起使用sql
這是不需要@ ZIEM。 – Vishal