我正在尝试查看是否有一种方法可以创建一个方法来为多个按钮实现触摸监听器,因为我有很多按钮几乎可以做同样的事情。他们所做的唯一区别是他们将通过我的 sendMessage() 方法发送的消息,以及需要按住按钮多长时间才能发送消息。如果有办法做到这一点,那可能是什么?而且,为什么这样的东西行不通?
//Within onCreate Method...
Button mButton = (Button) findViewbyId(R.id.three_sec_button);
mButton = addTouchTimer(mButton, 3, 3);
通话-
private Button addTouchTimer(Button button, final int sec, final int messageNum){
button.setOnTouchListener(new View.OnTouchListener() {
boolean longEnough = false;
long realTimeLeft = sec * 1000;
@Override
// This will make it so that a message is only sent if the button is held down for 3 seconds
// Otherwise it won't send. It is sent during the hold down process, releasing it returns a false
// value and no message is sent.
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.d("Button", "Touchy Touchy!");
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
buttonPressTime = new CountDownTimer(realTimeLeft, 1000){
@Override
public void onTick(long millisUntilDone){
realTimeLeft = millisUntilDone;
}
@Override
public void onFinish() {
long timeLeft = realTimeLeft;
long currTime = System.currentTimeMillis();
long realFinishTime = currTime + timeLeft;
while(currTime < realFinishTime){
currTime = System.currentTimeMillis();
}
longEnough = true;
sendEmergencyMessage(longEnough, messageNum);
}
}.start();
}
else if(arg1.getAction() == MotionEvent.ACTION_UP){
buttonPressTime.cancel();
sendMessage(longEnough, messageNum);
}
return longEnough;
}
});
return button;
}
似乎为了提高效率,必须有比为每个按钮实现单独的监听器更好的方法。请注意,sendMessage() 中有一个使用 bool 值的 Log 调用,我想看看它被传递时的设置。这是在释放按钮期间调用它的唯一原因。
最佳答案
是的,你是对的,有更好的方法。处理所有事情的单个 TouchListener,通过 id 确定它是哪个按钮。
void intialization(){
Button m1, m2, m3, m4;
... //do initialization stuff
m1.setId(1);
m2.setId(2);
m3.setId(3);
m4.setId(4);
MyTouchListener touchListener = new MyTouchListener();
m1.setOnTouchListener(touchListener);
m2.setOnTouchListener(touchListener);
m3.setOnTouchListener(touchListener);
m4.setOnTouchListener(touchListener);
}
public class MyTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case 1:
//do stuff for button 1
break;
case 2:
//do stuff for button 2
break;
case 3:
//do stuff for button 3
break;
case 4:
//do stuff for button 4
break;
}
return true;
}
}
这就是您要做的!在这种情况下,id 的数值方法非常有用。另一种方法是让您的 Activity 在您的 Activity 中实现 OnTouchListener,这样您的代码会更简单。
public class MyActivity extends Activity implements OnTouchListener {
void initialization(){
Button m1, m2, m3, m4;
... //do initialization stuff
m1.setId(1);
m2.setId(2);
m3.setId(3);
m4.setId(4);
m1.setOnTouchListener(this);
m2.setOnTouchListener(this);
m3.setOnTouchListener(this);
m4.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case 1:
//do stuff for button 1
break;
case 2:
//do stuff for button 2
break;
case 3:
//do stuff for button 3
break;
case 4:
//do stuff for button 4
break;
}
return true;
}
}
注意:此方法也适用于 OnClickListener、OnCheckedChangeListener 或您将在 Android View 上设置的任何其他监听器。
关于android - 为多个按钮实现 onTouchListener() 的单一方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864143/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案