0

我正在以编程方式设置 x 的值edittext。但是,这样做时,调整平移功能不起作用。关于如何完成这项工作的任何想法。activity不在全屏模式下,这只发生在APIs > 23.

前任。

Edittext editext = findViewById(R.id.edittext);
edittext.setX(200);

//edittext现在点击,如果它在屏幕上足够低,就会被软键盘覆盖

注意:这也发生在setY()

这是重现错误的示例:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context context = this;
        setContentView(R.layout.activity_main);

        //Get edittext from layout file and set X and Y to the bottom
        //left of screen
        EditText edittext = findViewById(R.id.editText);
        edittext.setX(0);

        //Getting device frame to make sure its below keyboard, Not
        //necessary can just guess any value 
        edittext.setY(getDeviceFrame(context).getHeight() / 1.2f);
    }

布局为:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

和清单:

<activity android:name=".MainActivity"
    android:windowSoftInputMode="adjustPan">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category 
                android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
</activity>

导致布局: 编程 setX 和 setY 之后的 Edittext

单击editext后,我们得到: 隐藏的editext

您可以看到它被键盘隐藏。

更新:我尝试使用不同的变体来移动 EditText 仍然无济于事。

前任。

edittext.animate().x(200);

更新2:我还没有解决这个问题。我已经越来越接近,因为我做了一些测试,似乎使用布局参数来控制视图的定位在一定程度上是有效的。

前任。

ConstraintLayout.LayoutParams layoutParams = 
    (ConstraintLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = 200;  //Your X Coordinate
layoutParams.topMargin = 200;  //Your Y Coordinate
view.setLayoutParams(layoutParams);

在某种程度上,我的意思是我在测试库中弄乱了这些方法的功能,它似乎可以从 onCreate() 执行上述操作。但是,在我的应用程序中,我正在回调中进行调用。SetX 工作正常,无需我在“post”线程或“runOnMainUI”线程中设置它。所以我不确定为什么这不起作用。

4

2 回答 2

-1

setXandsetY替换为leftMarginandtopMargin用于弹出定位,它将在大于 23 的 API 上按预期工作。

我无法解释原因,但 API 23 上不存在该问题。

如建议的那样,这里是用于通过拖动按钮移动弹出布局并保持调整平移工作的代码示例。

        final View myNote = getActivity().getLayoutInflater().inflate(R.layout.viewer_annotation_layout, null);
        final RelativeLayout.LayoutParams noteLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        myNote.setLayoutParams(noteLayoutParams);
        mainScreenLayout.addView(myNote, noteLayoutParams);


        btnmove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:

                        //addednotedX = myNote.getX() - event.getRawX();
                        //addednotedY = myNote.getY() - event.getRawY();

                        addednotedX = noteLayoutParams.leftMargin - event.getRawX();
                        addednotedY = noteLayoutParams.topMargin - event.getRawY();

                        break;
                    case MotionEvent.ACTION_MOVE:
                        int newposx = event.getRawX() + addednotedX;
                        int newposy = event.getRawY() + addednotedY;

                        /* this is move the Note layout but prevent adjsutpan working */
                        //myNote.setX(newposx);
                        //myNote.setY(newposy);


                        /* this is move the Note layout and keep adjsutpan working */
                        noteLayoutParams.leftMargin = newposx;
                        noteLayoutParams.topMargin =  newposy;
                        myNote.setLayoutParams(noteLayoutParams);

                        break;
                    default:
                        return false;

                }
                return false;
            }
        });
于 2020-04-08T18:18:53.207 回答
-1

好的,所以我想出了如何解决这个问题。使用上面我的“更新 2”中的答案,我能够通过一些调整获得解决方案。

这段代码:

ConstraintLayout.LayoutParams layoutParams = 
    (ConstraintLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = 200;  //Your X Coordinate
layoutParams.topMargin = 200;  //Your Y Coordinate
view.setLayoutParams(layoutParams);

(您可以使用您正在使用的任何布局而不是约束)

是 setX() 和 setY() 的替代方法,并允许 adjustPan 工作。但是,使用以下命令获取视图 x 或 y 定位:

view.getX();
view.getY();

不会返回视图的视觉定位。您需要通过获取与视图关联的布局参数并获取其边距来获取视图的位置。

现在我开始意识到约束布局的关键是你必须在 xml 中或通过代码定义约束。

前任。

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

有了这个,边距就可以引用它所扩展的内容,没有这个,视图的定位不移动。

于 2019-02-04T21:15:13.593 回答