首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中点击另一个编辑文本时,以编程方式创建的编辑文本也会获得焦点

问在android中点击另一个编辑文本时,以编程方式创建的编辑文本也会获得焦点
EN

Stack Overflow用户
提问于 2015-05-04 18:25:11
回答 2查看 51关注 0票数 0

我已经使用XML文件中现有的编辑文本以编程方式创建了多个编辑文本,但是当主编辑文本获得焦点时,动态创建的编辑文本也获得焦点。我的代码如下:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_worker_reg);
        existingContact = (EditText)findViewById(R.id.workerPhone);
        drawable = existingContact.getBackground();
    }

 public void addAnotherContactNumber(View view) {
        final CharSequence[] options = { "Work", "Home","Cancel" };
        AlertDialog.Builder builder = new     AlertDialog.Builder(WorkerRegActivity.this);
        builder.setTitle("Add Contact Number!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Work"))
                {
                    linearLayout =     (LinearLayout)findViewById(R.id.containerLayout);
                    EditText newContact = new EditText(WorkerRegActivity.this);
                    newContact.setHint("Phone NO." + (newContactIndex - 1));
                        newContact.setHintTextColor(existingContact.getHintTextColors());
                    newContact.setInputType(existingContact.getInputType());
                    newContact.setLayoutParams(existingContact.getLayoutParams());

                    int sdk = android.os.Build.VERSION.SDK_INT;
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        newContact.setBackgroundDrawable(drawable);
                    } else {
                        newContact.setBackground(drawable);
                    }

                    linearLayout.addView(newContact, newContactIndex);
                    newContactIndex += 1;

                    contactList.add(newContact);

                }
                else if (options[item].equals("Home"))
                {

                }
                else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

实际上,我想使用XML文件中定义的具有相同背景的现有编辑文本创建多个编辑文本,但是当一个编辑文本获得焦点时,另一个编辑文本也会自动获得焦点。请帮帮忙……

EN

回答 2

Stack Overflow用户

发布于 2015-05-04 18:28:43

当您添加新的editTexts时,只需添加一个关闭焦点的属性线:

代码语言:javascript
复制
edittext.clearFocus();

其中edittext是您的edittext的id。

如果这不起作用,您可以使用以下命令:

代码语言:javascript
复制
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->

<LinearLayout
    android:focusable="true" android:focusableInTouchMode="true"
    android:layout_width="0px" android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
     to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"/>

这只是一种变通办法,目的是诱使android将焦点放在编辑文本之外的其他东西上。但是请记住,您必须将虚拟元素放在要从中撤销焦点的editText之前。

票数 1
EN

Stack Overflow用户

发布于 2015-05-04 20:27:00

在做这样的事情时,我们应该使用不同的绘制对象,而不是相同的实例。

代码语言:javascript
复制
Drawable clone = drawable.getConstantState().newDrawable();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30027807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档