我有一条短信可以激活触控事件。现在,如果我的文本是空的或空的,或者它上显示了提示,那么Touch事件必须被停用。
text1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(text1.equals("")){
}
else{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
text1.setBackgroundColor(Color.RED);
InputConnection ic = getCurrentInputConnection();
ic.commitText(textOne, 1);
break;
case MotionEvent.ACTION_UP:
text1.setBackgroundColor(Color.YELLOW);
break;
}
}
return true;
}
}但是,当我在上述特定条件下触摸我的文本时,它会使屏幕崩溃,同时还会激活touch。有什么建议吗?
发布于 2014-07-01 06:47:07
我认为您的应用程序崩溃是因为如果text1为null,则在尝试以下操作时将出现空指针异常:if(text1.equals(""))
所以你应该试一试:
text1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(text1!=null && !text1.trim().equals("")){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
text1.setBackgroundColor(Color.RED);
InputConnection ic = getCurrentInputConnection();
ic.commitText(textOne, 1);
break;
case MotionEvent.ACTION_UP:
text1.setBackgroundColor(Color.YELLOW);
break;
}
}
return true;
}
}发布于 2014-07-01 09:03:32
最后我找到了答案。解决办法是:
if(text1.getText()!=null && !text1.getText().equals("")){
text1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
text1.setBackgroundColor(Color.RED);
InputConnection ic = getCurrentInputConnection();
ic.commitText(textOne, 1);
break;
case MotionEvent.ACTION_UP:
text1.setBackgroundColor(Color.YELLOW);
break;
}
return true;
}
});}
https://stackoverflow.com/questions/24503914
复制相似问题