首页 \ 问答 \ 根据长度读取线条(Read lines based on their length)

根据长度读取线条(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 ...
  • 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)