android 编辑文本焦点不起作用

发布于 2024-12-10 08:51:06 字数 952 浏览 1 评论 0原文

我有一个布局,其中 EditText 是通过按“创建 EditText”按钮动态填充的。

    public void onCreateButtonPressed(View view) {
     EditText et = new EditText(this);
     et.setOnFocusChangeListener(new OnFocusChangeListener() {
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
             int id = v.getId();
             Log.d(TAG, "EditText: "+id+"Focus state:"+hasFocus);
         }
     });
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
     et.setFocusableInTouchMode(true);
     et.setFocusable(true);
     et.requestFocus();
     et.setId(id++);
     layout.addView(et, params);
 }

它运行良好。但是,当我将焦点更改为某些编辑文本并按“创建 EditText”按钮时,光标在新创建的 EditText 中闪烁,但我键入的文本未显示新创建的 EditText。在Google和SOF中搜索给出了requestFocus()函数的用法。但我已经在新创建的 EditText 中使用 requestFocus() 。

我正在拼命寻找这个问题的解决方案。欢迎任何建议。

-问候, 罗恩

I have a layout in that EditText is populated dynamically by pressing "Create EditText" Button.

    public void onCreateButtonPressed(View view) {
     EditText et = new EditText(this);
     et.setOnFocusChangeListener(new OnFocusChangeListener() {
         @Override
         public void onFocusChange(View v, boolean hasFocus) {
             int id = v.getId();
             Log.d(TAG, "EditText: "+id+"Focus state:"+hasFocus);
         }
     });
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
     et.setFocusableInTouchMode(true);
     et.setFocusable(true);
     et.requestFocus();
     et.setId(id++);
     layout.addView(et, params);
 }

It is working good. But when i change the focus to some edit text and press "Create EditText" Button then the cursor is blinking in the newly created EditText but the text i typed is not showing the newly created EditText. Searching in Google and SOF gives the usage of requestFocus() function. But i am already using requestFocus() in the newly created EditText.

I am desperately searching the solution for this problem. Any suggestions are welcomed.

-Regards,
Ron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-12-17 08:51:06

实际上我发现了确切的问题,在早期的代码中我请求焦点,然后将视图添加到布局中。但我们需要反其道而行之。首先,需要将视图添加到布局中,然后将 requestFocus 添加到编辑文本中。这就是解决问题。

-问候,
罗恩..

Actually i found out the exact problem, in the earlier code i was requesting focus and then adding view to the layout. But we need to do the opposite. First the view needs to be added to the layout and then requestFocus to the edit text. That is solving the problem.

-Regards,
Ron..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文