我已经使用XML文件中现有的编辑文本以编程方式创建了多个编辑文本,但是当主编辑文本获得焦点时,动态创建的编辑文本也获得焦点。我的代码如下:
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文件中定义的具有相同背景的现有编辑文本创建多个编辑文本,但是当一个编辑文本获得焦点时,另一个编辑文本也会自动获得焦点。请帮帮忙……
发布于 2015-05-04 18:28:43
当您添加新的editTexts时,只需添加一个关闭焦点的属性线:
edittext.clearFocus();其中edittext是您的edittext的id。
如果这不起作用,您可以使用以下命令:
<!-- 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之前。
发布于 2015-05-04 20:27:00
在做这样的事情时,我们应该使用不同的绘制对象,而不是相同的实例。
Drawable clone = drawable.getConstantState().newDrawable();https://stackoverflow.com/questions/30027807
复制相似问题