【问题标题】:How create customview touch the same timerpicker?如何创建自定义视图触摸同时选择器?
【发布时间】:2018-08-23 18:07:46
【问题描述】:

我想创建一个视图相同的图像。 查看上的触摸值:

我使用解决方案创建 TextView 并触摸 TextView: 这是布局 xml:我创建了 6 个 TextView 以在触摸屏幕时显示值。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">    
            <TextView
                android:id="@+id/txtPreSelect1"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"                   
                android:gravity="center"                   
                android:text="000"                     
                />

            <TextView
                android:id="@+id/txtPreSelect2"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="0"
              />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"                
            >    
            <TextView
                android:layout_width="200dp"
                android:layout_height="1dp"
                 />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txtSelect1"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"
                android:layout_marginRight="@dimen/d_40"
                android:gravity="center"
                android:text="001"
               />

            <TextView
                android:id="@+id/txtSelect2"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="1"
               />
        </LinearLayout>    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <TextView
                android:layout_width="200dp"
                android:layout_height="1dp"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/txtNextSelect1"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"
                android:layout_marginRight="@dimen/d_40"
                android:gravity="center"
                android:text="002"
               />

            <TextView
                android:id="@+id/txtNextSelect2"
                android:layout_width="@dimen/d_60"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="2"
                />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"               
            android:gravity="center|right"
            android:orientation="horizontal">  

            <TextView
                android:id="@+id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="40dp"                   
                android:gravity="center"
                android:text="@string/btnCancel"/>

            <TextView
                android:id="@+id/btnOK"
                android:layout_width="wrap_content"
                android:layout_height="40dp"                                    
                android:text="@string/btnOK"
               />
        </LinearLayout>
    </LinearLayout>

我在 TextViews 上创建事件 Touch,以更改 Textview 的值:

dialog.txtPreSelect1.setOnTouchListener {v: View, event: MotionEvent ->
        // Perform tasks here
        when (event.action)
        {
            MotionEvent.ACTION_DOWN ->{
                yValue=event.y
            }
            MotionEvent.ACTION_MOVE ->{
                val curentY=event.y
                if(curentY>yValue)
                {
                    if(curentY>yValue) {
                        yValue=curentY
                        var iStartValue=dialog.txtPreSelect1.text.toString().toInt() - iStep1
                        
                            dialog.txtPreSelect1.text = iStartValue.toString()
                       
                        iStartValue=iStartValue +iStep1
                        dialog.txtSelect1.text = iStartValue.toString()
                        iStartValue=iStartValue +iStep1                                                            dialog.txtNextSelect1.text = iStartValue.toString()
                        
                    }
                }
                else if(curentY<yValue)
                {
                    if(curentY<yValue) {
                        yValue=curentY
                        var iStartValue=dialog.txtPreSelect1.text.toString().toInt() + iStep1
                       
                         dialog.txtPreSelect1.text = iStartValue.toString()
                     
                        iStartValue += iStep1
                        dialog.txtSelect1.text = iStartValue.toString()                           
                        dialog.txtNextSelect1.text = iStartValue.toString()
                        
                    }

                }
            }
        }
        true
    }

如何创建事件移动文本相同的图像 2? 或者 Exist other View 可以处理我的工作? 谢谢大家。

【问题讨论】:

    标签: android


    【解决方案1】:

    如果我理解正确,您的问题是用列表创建两个视图,因此您可以分别触摸和滚动它们。

    您可以尝试深入了解与您尝试访问的内容非常相似的库:SingleDateAndTimePicker

    【讨论】:

      【解决方案2】:

      我最好的猜测是你想使用原生组件。您可以使用ListView,但这需要比Number Picker 更多的代码。 Number Picker 专为处理此类情况而设计。

      在这里,Nice example 可以向您展示如何在对话框中使用它。基本上你需要一个水平方向的LinearLayout,并在其中放置两个Number Picker

      您可以像这样在 java/kotlin 的帮助下设置 MinValueMaxValue

      numberPicker.setMaxValue(50);
      numberPicker.setMinValue(1);
      

      如果您有任何疑问,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-11
        • 2010-11-19
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多