2016-08-10 6 views
0

このレイアウトは、テキストビューの名前を変更するためのポップアップウィンドウに使用されます。ただし、edittextは、edittextをクリックしてもキーボードを表示しません。私は断片でそれを使用しています。フラグメントには、このポップアップで[OK]をクリックすると変更されるネームフィールドがあります。EditTextポップアップでキーボードを開けません

これはポップアップウィンドウの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> 

これは、上記のXMLのためのJavaファイルです。

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

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); 
       } 
      }); 
    } 
}); 
ために、次の試みることができるあなたの EditText RelativeLayout0dp幅と layout_weight属性を持っています、それは意味をなさない。

どちらかEditTextLinearLayoutまたはEditTextからlayout_weight属性を削除し、それを適切な幅与えるの親ます次のコードを

<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はを持っていると仮定されていません幅が、幅がlayout_weight、幅が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"/> 

そしてEditTextは、それ自体でクリックの世話をするためにあなたのOnClickを変更します。

​​

をそして最後に、あなたのOnCreate()方法から以下の行を削除します。

name_change.setOnClickListener(this); 

EditTextは、クリックするとキーボードが単独で開きます。

関連する問題