【问题标题】:after to close a dialog, the keyboard do not dissappear关闭对话框后,键盘不消失
【发布时间】:2014-01-15 17:09:46
【问题描述】:

你好,我有一个活动,我从中调用了一个对话框。当对话框出现时,我在edittext中写了一些东西,然后单击保存按钮...问题是在关闭对话框并返回活动后,键盘仍然存在¡¡

我已经尝试过这样的解决方案:

a) 在布局(xml)中:

android:imeOptions="actionDone"

b) 在布局(xml)中:

android:focusable="true" 
android:focusableInTouchMode="true" 

c) 在清单中:

android:windowSoftInputMode="stateHidden"

但这不起作用...关闭对话框后键盘不会消失。

请提供一些线索来解决我的问题?

我的对话代码:

public class DialogCreamodListas extends Dialog
implements OnClickListener 
{
    static EditText etxLISTArecep;
    static EditText etxPRESUPUESTOrecep;
    ImageView mImageViewImagen1;
    ImageView mImageViewImagen2;
    Button btnAceptar;
    Context mContext;
    static Long ID_LISTA;
    static DbAdapter mDbHelper; 

    public DialogCreamodListas(Context context, long ID_LISTA, DbAdapter mDbHelper){    
            super(context);
            mContext = context;
            LayoutInflater inflater = LayoutInflater.from(mContext);
            final View view = inflater
                    .inflate(R.layout.dialogcreamodlist, null);
            setContentView(view);
            DialogCreamodListas.ID_LISTA = (long) ID_LISTA;
            DialogCreamodListas.mDbHelper = mDbHelper; 

            this.setTitle("Crea o modifica una lista");
            mImageViewImagen1 = (ImageView) this.findViewById(R.id.image1);
            mImageViewImagen1.setImageResource(R.drawable.mascarrito);
            mImageViewImagen2 = (ImageView) this.findViewById(R.id.image2);
            mImageViewImagen2.setImageResource(R.drawable.edit2);
            etxLISTArecep = (EditText) findViewById(R.id.etxNombreLista);
            etxPRESUPUESTOrecep = (EditText) findViewById(R.id.etxPresupuesto);
            CargaInformacion(); 

            btnAceptar = (Button) findViewById(R.id.btn_aceptar);

            btnAceptar.setOnClickListener(this);    
    }

    private void CargaInformacion()
    {
            if(ID_LISTA != null && ID_LISTA != -1)
            {
                    Cursor Lista = mDbHelper.RecuperaRegistros(DbAdapter.TABLA_LISTAS,ID_LISTA);
                    ((Activity) mContext).startManagingCursor(Lista);
                    etxLISTArecep.setText(Lista.getString(Lista.getColumnIndexOrThrow(DbAdapter.LISTA)));
                    etxPRESUPUESTOrecep.setText(Lista.getString(Lista.getColumnIndexOrThrow(DbAdapter.PRESUPUESTO)));
            }
    }    

    public void onClick(View v) {
        if (v == btnAceptar) 
            {
                //AQUI RECUPERO LOS DATOS Y ALMACENO EN BBDD 
                GuardaDatos();
                CargarDatos();
                dismiss();
                return;
            }
        }   

        public static void GuardaDatos()
        {
            try
            {
            String[] Valores;

            if(ID_LISTA == null || ID_LISTA == -1)
            {
                Valores = new String[]{null,etxLISTArecep.getText().toString(),etxPRESUPUESTOrecep.getText().toString()};
                long id= mDbHelper.creaRegistro(DbAdapter.TABLA_LISTAS,Valores);
                if(id>0)
                {
                    ID_LISTA=id;
                }
            }
            else
            {   Valores = new String[]{ID_LISTA.toString(),etxLISTArecep.getText().toString(),etxPRESUPUESTOrecep.getText().toString()};    
                mDbHelper.actualizaRegistro(DbAdapter.TABLA_LISTAS,Valores);
            }   
            }
            catch(Exception E)
            {
                Log.e("EditaListas","Error: "+E);
            }
        }   

        public void CargarDatos()
        {
            if (this.mContext instanceof AdminListas)   
            {
                ((AdminListas) this.mContext).CargaDatos();
            }
            else
            {   
                Cursor ListaCursor=  mDbHelper.RecuperaRegistrosTabla(DbAdapter.VISTA_LISTAS);
                ((Activity) this.mContext).startManagingCursor(ListaCursor);        
                String[] Origen = new String[]{DbAdapter.LISTA,DbAdapter.PRESUPUESTO,DbAdapter.ARTICULOS,DbAdapter.DIFERENCIA};
                int [] Destino = new int[]{R.id.txvnombrelista,R.id.txvpresupuesto,R.id.txvarticulos,R.id.txvdiferencia};       
                SimpleCursorAdapter listas = new SimpleCursorAdapter(this.getContext(),R.layout.registrodetallelista,ListaCursor,Origen,Destino);       
                ((ListActivity) this.mContext).setListAdapter(listas);  
                }
        }


}

【问题讨论】:

  • 你的对话代码在哪里?我认为您可能会自己忽略解雇!
  • 我已经用代码编辑了帖子;)

标签: android dialog


【解决方案1】:

要手动关闭软键盘,您可以这样做:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

editText 是您的软键盘当前正在与之交互的 EditText。

【讨论】:

  • 当我将代码放在我的 .java 中时,我得到一个错误编译 getSystemService 没有定义到我的对话框中。我认为这项工作适用于活动,但我怎样才能在我的对话框中使 getSystemService 工作?
  • 好的,最后这是编译但不起作用的代码:((InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(etxLISTArecep.getWindowToken(), 0);
【解决方案2】:

要使键盘消失,有必要在清单中调用您的对话框的活动中删除跟随参数

android:windowSoftInputMode...

我有这个应该隐藏键盘

android:windowSoftInputMode="stateHidden"

但是当我删除了整行时它可以工作¡¡¡¡¡¡:

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 2019-11-16
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多