根据长度读取线条(Read lines based on their length)
我想知道更有效的方法只能读取基于一定长度的文件的字符串。 我知道这是一种天真的方法:
len = 3 d = Array.new t = File.open('d.txt').read t.each_line do |x| #+2 accounting for \n\r if x.length == (len + 2) d.push(x) end end
I was wondering of more efficient way to only read the strings of a file based on a certain length. I know this is a naive approach:
len = 3 d = Array.new t = File.open('d.txt').read t.each_line do |x| #+2 accounting for \n\r if x.length == (len + 2) d.push(x) end end
原文:https://stackoverflow.com/questions/15317180
更新时间:2022-02-19 06:02
最满意答案
我有解决方案
// TODO Auto-generated method stub int X = (int) event.getRawX(); int Y = (int) event.getRawY(); layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v .getLayoutParams(); _xDelta = X - lParams.leftMargin; _yDelta = Y - lParams.topMargin; startTime = System.currentTimeMillis(); if (upCounter != 0) { timeBetweenTwoClick = startTime - endTime; } mSingleStart = System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: endTime = System.currentTimeMillis(); mSingleEnd = System.currentTimeMillis(); currentClickTime = endTime - startTime; consecutiveTwoClickTime = currentClickTime + previousClickTime; // textview single tap detection if (mSingleEnd - mSingleStart <= mSingleTimeDiffrence) { Toast.makeText(MainActivity.this, "tap", 1500).show(); if (mSingleCount == false) { GradientDrawable gd = new GradientDrawable(); gd.setCornerRadius(5); gd.setStroke(3, 0xFF000000); text.setBackgroundDrawable(gd); verticalSeekBar.setVisibility(View.VISIBLE); verticalSeekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch( SeekBar seekBar) { // TODO Auto-generated method // stub } @Override public void onStartTrackingTouch( SeekBar seekBar) { // TODO Auto-generated method // stub } @Override public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method // stub text.setTextSize(progress + 20); text2.setTextSize(progress + 20); } }); mSingleCount = true; } else { text.setBackgroundDrawable(null); mSingleCount = false; verticalSeekBar.setVisibility(View.INVISIBLE); } } if (upCounter == 0) { ++upCounter; previousClickTime = currentClickTime; } else { previousClickTime = currentClickTime; if ((consecutiveTwoClickTime + timeBetweenTwoClick) <= doubleClickTimeDiffrence) { if (layoutParams != null) text2.setLayoutParams(layoutParams); text2.setVisibility(View.VISIBLE); text2.requestFocus(); if (text2.getText().toString() .equalsIgnoreCase("enter by double tap")) { text2.setText(""); } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT); text.setVisibility(View.INVISIBLE); Toast.makeText(MainActivity.this, "double tap", 1500).show(); } } break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_POINTER_UP: break; case MotionEvent.ACTION_MOVE: layoutParams.leftMargin = X - _xDelta; layoutParams.topMargin = Y - _yDelta; if (layoutParams.leftMargin < 0) { layoutParams.leftMargin = 20; } else if (layoutParams.leftMargin > mainimage.getWidth()) { layoutParams.leftMargin = mainimage.getWidth() - 20; } if (layoutParams.topMargin < 0) { layoutParams.topMargin = 20; } else if (layoutParams.topMargin > mainimage.getHeight() - 100) { text.setText(text2.getText().toString()); layoutParams.topMargin = mainimage.getHeight() - 90 - text.getHeight(); // layoutParams.leftMargin = 20; } text.setX(layoutParams.leftMargin); text.setY(layoutParams.topMargin); v.setLayoutParams(text.getLayoutParams()); Log.i("mainimage.getHeight() ", (mainimage.getHeight() - 60) + " " + layoutParams.topMargin + " " + text.getHeight()); // if (text.getLeft() >= 0 && text.getTop() >= 0 // && text.getRight() < width // && text.getBottom() < mainimage.getHeight()) { // // text.setX(layoutParams.leftMargin); // text.setY(layoutParams.topMargin); // } else if (text.getTop() < 0) { // // text.setX(layoutParams.leftMargin); // text.setY(10); // } else if (text.getBottom() > mainimage.getHeight()) { // text.setX(layoutParams.leftMargin); // text.setY(mainimage.getHeight() - 50); // } else if (text.getLeft() < 0) { // text.setX(10); // text.setY(layoutParams.topMargin); // } else if (text.getRight() > mainimage.getWidth()) { // // text.setX(mainimage.getWidth() - 10); // text.setY(layoutParams.topMargin); // } // else { // text.setX(20); // text.setY(20); // } // layoutParams.rightMargin = -250; // layoutParams.bottomMargin = -250; // if (text.getWidth() < (width - 50)) { // // } // text.setWidth(width - text.getLeft()); // Log.i("From Current position: ", // "Left Margin: " + X + " " + _xDelta // + "Left Margin: " + Y + " " + _yDelta // + "layoutParams.leftMargin: " // + layoutParams.leftMargin // + " layoutParams.topMargin: " // + layoutParams.topMargin // + " layoutParams.height - width: " // + layoutParams.height + " " + width // + " " + (width - text.getWidth()) + " " // + text.getWidth()); break;
i have got the solution
// TODO Auto-generated method stub int X = (int) event.getRawX(); int Y = (int) event.getRawY(); layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v .getLayoutParams(); _xDelta = X - lParams.leftMargin; _yDelta = Y - lParams.topMargin; startTime = System.currentTimeMillis(); if (upCounter != 0) { timeBetweenTwoClick = startTime - endTime; } mSingleStart = System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: endTime = System.currentTimeMillis(); mSingleEnd = System.currentTimeMillis(); currentClickTime = endTime - startTime; consecutiveTwoClickTime = currentClickTime + previousClickTime; // textview single tap detection if (mSingleEnd - mSingleStart <= mSingleTimeDiffrence) { Toast.makeText(MainActivity.this, "tap", 1500).show(); if (mSingleCount == false) { GradientDrawable gd = new GradientDrawable(); gd.setCornerRadius(5); gd.setStroke(3, 0xFF000000); text.setBackgroundDrawable(gd); verticalSeekBar.setVisibility(View.VISIBLE); verticalSeekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch( SeekBar seekBar) { // TODO Auto-generated method // stub } @Override public void onStartTrackingTouch( SeekBar seekBar) { // TODO Auto-generated method // stub } @Override public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method // stub text.setTextSize(progress + 20); text2.setTextSize(progress + 20); } }); mSingleCount = true; } else { text.setBackgroundDrawable(null); mSingleCount = false; verticalSeekBar.setVisibility(View.INVISIBLE); } } if (upCounter == 0) { ++upCounter; previousClickTime = currentClickTime; } else { previousClickTime = currentClickTime; if ((consecutiveTwoClickTime + timeBetweenTwoClick) <= doubleClickTimeDiffrence) { if (layoutParams != null) text2.setLayoutParams(layoutParams); text2.setVisibility(View.VISIBLE); text2.requestFocus(); if (text2.getText().toString() .equalsIgnoreCase("enter by double tap")) { text2.setText(""); } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT); text.setVisibility(View.INVISIBLE); Toast.makeText(MainActivity.this, "double tap", 1500).show(); } } break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_POINTER_UP: break; case MotionEvent.ACTION_MOVE: layoutParams.leftMargin = X - _xDelta; layoutParams.topMargin = Y - _yDelta; if (layoutParams.leftMargin < 0) { layoutParams.leftMargin = 20; } else if (layoutParams.leftMargin > mainimage.getWidth()) { layoutParams.leftMargin = mainimage.getWidth() - 20; } if (layoutParams.topMargin < 0) { layoutParams.topMargin = 20; } else if (layoutParams.topMargin > mainimage.getHeight() - 100) { text.setText(text2.getText().toString()); layoutParams.topMargin = mainimage.getHeight() - 90 - text.getHeight(); // layoutParams.leftMargin = 20; } text.setX(layoutParams.leftMargin); text.setY(layoutParams.topMargin); v.setLayoutParams(text.getLayoutParams()); Log.i("mainimage.getHeight() ", (mainimage.getHeight() - 60) + " " + layoutParams.topMargin + " " + text.getHeight()); // if (text.getLeft() >= 0 && text.getTop() >= 0 // && text.getRight() < width // && text.getBottom() < mainimage.getHeight()) { // // text.setX(layoutParams.leftMargin); // text.setY(layoutParams.topMargin); // } else if (text.getTop() < 0) { // // text.setX(layoutParams.leftMargin); // text.setY(10); // } else if (text.getBottom() > mainimage.getHeight()) { // text.setX(layoutParams.leftMargin); // text.setY(mainimage.getHeight() - 50); // } else if (text.getLeft() < 0) { // text.setX(10); // text.setY(layoutParams.topMargin); // } else if (text.getRight() > mainimage.getWidth()) { // // text.setX(mainimage.getWidth() - 10); // text.setY(layoutParams.topMargin); // } // else { // text.setX(20); // text.setY(20); // } // layoutParams.rightMargin = -250; // layoutParams.bottomMargin = -250; // if (text.getWidth() < (width - 50)) { // // } // text.setWidth(width - text.getLeft()); // Log.i("From Current position: ", // "Left Margin: " + X + " " + _xDelta // + "Left Margin: " + Y + " " + _yDelta // + "layoutParams.leftMargin: " // + layoutParams.leftMargin // + " layoutParams.topMargin: " // + layoutParams.topMargin // + " layoutParams.height - width: " // + layoutParams.height + " " + width // + " " + (width - text.getWidth()) + " " // + text.getWidth()); break;
相关问答
更多-
尝试这个 final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onDoubleTap(Motio ...
-
双击注释视图(Double tap on annotation view)[2023-08-10]
1)在-mapView viewForAnnotation if ([annotationView gestureRecognizers] == nil) { UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapOnAnnotationView:)]; tapGesture.numberOfTapsR ... -
你为什么不用长按? 或者你是否已经使用了其他的东西? 双重触摸的优势: Long Press是UI指南中推荐的互动,Double Touch不是。 这是用户期望的 用户可能找不到Double Touch操作,因为它们不会去寻找 它已经在API中处理 。 实施双重触摸将会影响单触摸的处理,因为您必须等待每个单一触摸是否变为双触,才能处理它。 Why aren't you using a Long Press? Or are you using that already for something else? ...
-
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease]; singleTap.numberOfTapsRequired = 1; [self.view addGestureRecognizer:singleTap]; UITapGestureRecognizer *doubleTap = [[[U ...
-
您已经在单击事件处理程序中,并且您正在接收单击的View作为参数,因此无需再次找到它。 这应该启动Activity。 public void signInButton(View view) { Intent showSignInActivity = new Intent(v.getContext(), SignInActivity.class); startActivity(showSignInActivity); } You are already in the click event ...
-
这听起来像只有当触摸不滚动滚动视图时,您才希望轻敲识别器成功。 这很容易,因为滚动视图使用平移手势识别器进行滚动。 在iOS 5上,你可以这样做: [self.tapRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer]; 如果你想支持较旧版本的iOS,你必须这样做: for (UIGestureRecognizer *recognizer in self.scrollView.gestureRecog ...
-
我有解决方案 // TODO Auto-generated method stub int X = (int) event.getRawX(); int Y = (int) event.getRawY(); layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams(); switch (event.getAction() & MotionEvent ...
-
对于遇到此问题的任何人。 我最后做的是用GestureListener来听长时间的压力。 触发长按事件后,我使用registerForContextMenu(View v)注册上下文菜单的视图,然后调用openContextMenu(View v)。 并且为了不弹出上下文菜单并干扰长按事件,我覆盖onContextMenuClosed(菜单菜单)方法,其中我调用unregisterForContextMenu(View v),传递我之前注册的视图。 For anyone who has run into t ...
-
使用http://developer.android.com/reference/android/view/GestureDetector.html 例如: public class AndroidTestActivity extends Activity { private GestureDetector gestureDetector; @Override public boolean onTouchEvent(MotionEvent event) { ret ...
-
好吧,这很笨拙,但请尝试以下方法: private boolean singleTapped = false; private long lastTapTime = 0; private long doubleTapInterval = 1000; @Override public void onTouchEvent(MotionEvent event) { if (!singleTapped) { singleTapped = true; lastTapTime ...