[英]Button android while pressed
I want to print something while my button is pressed (not after it is released). 我想在按下按钮时(而不是在释放按钮后)打印一些东西。 At moment I have this, but it only works once... 现在我有这个,但是它只能工作一次...
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println("pressed");
return true;
}
return false;
}
});
try this to continously print pressed message while you are pressing it. 尝试此操作以在按下时连续打印按下的消息。
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("pressed");
return false;
}
});
System.out doesn't work in an Android Device (won't show you anything on the device) and if you have a text view you can set the text on your MotionEvent.ACTION_DOWN
as you are already doing and on the MotionEvent.ACTION_UP
you set your text of your text view to empty. System.out在Android设备上不起作用(不会在设备上显示任何内容),并且如果您具有文本视图,则可以像以前一样在MotionEvent.ACTION_UP
和MotionEvent.ACTION_DOWN
上设置文本您将文本视图的文本设置为空。 Something like this: 像这样:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
final TextView textView = (TextView) findViewById(R.id.textview);
final Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
textView.setText("Button Pressed");
}
if(event.getAction() == MotionEvent.ACTION_UP){
textView.setText(""); //finger was lifted
}
return true;
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.