2016-08-10 6 views
0

Этот макет используется для всплывающего окна, чтобы изменить имя textview. Но edittext не показывает клавиатуру даже при нажатии на edittext. Я использую его в фрагменте. фрагмент имеет поле имени, которое должно быть изменено при нажатии OK во всплывающем окне.EditText doen't open keyboard in popup

Это xml-файл для всплывающего окна.

<RelativeLayout 

xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fff"> 


    <LinearLayout 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/name_status_btns" 
    > 

    <Button 

     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:elevation="2dp" 
     android:background="#fff" 
     android:text="CANCEL" 
     android:textSize="18sp" 
     android:textColor="#000" 
     android:id="@+id/cancel_change_name"/> 


    <TextView 

     android:layout_width="1dp" 
     android:layout_height="match_parent" 
     android:background="#b1b0b0"/> 


    <Button 

     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="OK" 
     android:textColor="#000" 
     android:textSize="18sp" 
     android:elevation="2dp" 
     android:background="#fff" 
     android:id="@+id/ok_change_name" 
     /> 


    </LinearLayout> 


<TextView 

    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="#b1b0b0" 
    android:layout_above="@id/name_status_btns" 
    android:id="@+id/name_status_horizontal_liner"/> 


    <RelativeLayout 

    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_marginTop="15dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:id="@+id/name_status_edit_field"> 


    <EditText 

     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:hint="write your name here" 
     android:scrollHorizontally="true" 
     android:id="@+id/change_name" 
     android:focusable="true" 
     android:focusableInTouchMode="true"/> 


    </RelativeLayout> 



    <TextView 

    android:layout_width="match_parent" 
    android:layout_height="1dp" 
    android:background="@color/mainColor" 
    android:layout_below="@id/name_status_edit_field" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 

    /> 

    </RelativeLayout> 

Это файл Java для выше XML.

public class Name_Status extends AppCompatActivity implements View.OnClickListener { 

EditText name_change; 
RelativeLayout name_status_edit_field; 
String name; 
Button cancel_name_change , ok_name_change; 
@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.name_status); 
    DisplayMetrics dm=new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    int width=dm.widthPixels; 
    int height=dm.heightPixels; 

    getWindow().setLayout((int)(width*.8),(int)(height*.3)); 
    name_change=(EditText)findViewById(R.id.change_name); 
    name_status_edit_field=(RelativeLayout)findViewById(R.id.name_status_edit_field); 
    name=name_change.getText().toString(); 
    cancel_name_change=(Button)findViewById(R.id.cancel_change_name); 
    ok_name_change=(Button)findViewById(R.id.ok_change_name); 
    ok_name_change.setOnClickListener(this); 
    name_change.setOnClickListener(this); 
    name_status_edit_field.clearFocus(); 
    name_change.setFocusable(true); 
    //name_change.update(); 
} 

@Override 
public void onClick(View v) { 
      if(v.getId()==R.id.ok_change_name) 
      {FragActivity1 obj=new FragActivity1(); 
    obj.changeName(name); 
    Intent intent=new Intent(); 
    intent.setClass(Name_Status.this , MainActivity.class); 
    startActivity(intent);} 
    if(v.getId()==R.id.change_name) 
    { 
     InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0); 
    } 

} 
} 
+0

Пожалуйста, проверьте мой ответ. – himanshu1496

ответ

1

Основной проблемой является ваш EditText имеет 0dp ширину и атрибут layout_weight в RelativeLayout, что не имеет никакого смысла.

Либо сделать родителем EditTextLinearLayoutили удалить атрибут layout_weight из EditText и дать ему надлежащую ширину:

<EditText 
    android:id="@+id/change_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="write your name here" 
    android:scrollHorizontally="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true"/> 

Следующий код является совершенно ненужным, вы должны просто удалить его из onClick():

if(v.getId()==R.id.change_name) 
{ 
    InputMethodManager imm = (InputMethodManager) getSystemService(Context. 
     INPUT_METHOD_SERVICE); 
    imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(), 
     InputMethodManager.SHOW_FORCED, 0); 
} 
0

Вы также можете попробовать следующее за EditText

editText.setOnFocusChangeListener(new OnFocusChangeListener() { 

    @Override 
    public void onFocusChange(final View v, final boolean hasFocus) { 
     if (hasFocus && editText.isEnabled() && editText.isFocusable()) 
      editText.post(new Runnable() { 
       @Override 
       public void run() { 
        final InputMethodManager imm = (InputMethodManager) context 
          .getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
       } 
      }); 
    } 
}); 

Для более подробной информации см Keyboard not shown when i click on edittextview in android?

+0

Что случилось с моим кодом? –

0

EditText не предполагают, чтобы иметь 0dp ширина и layout_weight внутри a RelativeLayout. Вес работает внутри LinearLayout. Таким образом, изменить EditText ниже:

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="write your name here" 
    android:scrollHorizontally="true" 
    android:id="@+id/change_name" 
    android:focusable="true" 
    android:focusableInTouchMode="true"/> 

и изменить свой OnClick, потому EditText будет заботиться клик сам по себе:

@Override 
public void onClick(View v) { 
if(v.getId()==R.id.ok_change_name){ 
    FragActivity1 obj=new FragActivity1(); 
    obj.changeName(name); 
    Intent intent=new Intent(); 
    intent.setClass(Name_Status.this , MainActivity.class); 
    startActivity(intent); 
} 
} 

И наконец, удалить строку ниже из вашего OnCreate() метода:

name_change.setOnClickListener(this); 

Потому что EditText откроет клавиатуру сам по себе при нажатии.