Lowrie zt 2021-07-13 15:52 采纳率: 0%
浏览 84

android edittext获取焦点立马跳转到按钮上了

img

XML

        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:id="@+id/ly2"
        android:orientation="vertical"
        tools:ignore="MissingConstraints">
        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/ly1"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            tools:ignore="MissingConstraints">

            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="条码"
                android:imeOptions="actionNext"
                android:nextFocusForward="@+id/password"
                />
            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="密码"
                android:inputType="textPassword" />

            <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="清理库存"></Button>
        </LinearLayout>

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true">

            <ListView
                android:id="@+id/list_viewstockbill"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></ListView>
        </HorizontalScrollView>
    </LinearLayout>

Activity

    EditText password;
    ListView listView;
    private void ValLoginWindow() {
        try {
            list = new ArrayList<>();
            for (int i= 0 ;i < 3;i++){
                OutDeport outDeport = new OutDeport();
                outDeport.setWis_BatchNo("物料批号");
                outDeport.setWis_DateCode("DateCode");
                list.add(outDeport);
            }
            final View v = getLayoutInflater().inflate(R.layout.tab_view, null);
            listView = v.findViewById(R.id.list_viewstockbill);
            adapter = new DialogStockBillAdapter(this, R.layout.dialog_list_del,list );
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final OutDeport outDeport = list.get(position);
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("提示:");
                    dialog.setMessage("是否删除?");
                    dialog.setCancelable(false);
                    dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            list.clear();
                            adapter.notifyDataSetChanged();
                        }
                    });
                    dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    dialog.show();
                }
            });

            final EditText username = v.findViewById(R.id.username);
            username.requestFocus();

            password = v.findViewById(R.id.password);
            username.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int i, KeyEvent keyEvent) {
                    if (keyEvent.getAction() == KeyEvent.KEYCODE_BACK) {
                        finish();
                    } else if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                        return true;
                    } else if (i == KeyEvent.KEYCODE_ENTER) {
                  
                        Toast.makeText(MainActivity.this,"你点击了回车",Toast.LENGTH_SHORT).show();
                        password.setCursorVisible(true);
                        password.requestFocus();
                        return true;
                    }
                    return false;
                }
            });
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setView(v);
            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });
            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }

            });

            AlertDialog dialog = builder.create();
            WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes();
            wlp.gravity = Gravity.TOP | Gravity.LEFT;
            wlp.x = 10;
            wlp.y = 10;
            dialog.show();
            dialog.getWindow().setLayout(400, 700);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


这个页面是一个按钮弹出的弹出框页面,然后我想条码输入完回车之后光标聚焦在密码位置,结果光标在密码框一闪而过,就聚焦到按钮上面了.怎么回事啊!

  • 写回答

1条回答 默认 最新

  • 帅次 移动开发领域新星创作者 2021-07-14 10:30
    关注

    在条码的EditText上新增设置singLine=“true”尝试一下

    如有帮助,麻烦采纳

    评论

报告相同问题?

问题事件

  • 创建了问题 7月13日

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作