EditText不会在弹出窗口中打开键盘

时间:2016-08-10 08:15:08

标签: android android-layout

此布局用于弹出窗口以更改textview的名称。但即使单击edittext,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);
    }

}
}

2 个答案:

答案 0 :(得分:1)

主要问题是EditText0dp中有layout_weight宽度和RelativeLayout属性,这没有任何意义。

EditText成为LinearLayout 的父级或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);
}

答案 1 :(得分:0)

您还可以对EditText

进行以下尝试
>>> df.filter(df.age > 3).collect()
[Row(age=5, name=u'Bob')]
>>> df.where(df.age == 2).collect()
[Row(age=2, name=u'Alice')]

>>> df.filter("age > 3").collect()
[Row(age=5, name=u'Bob')]
>>> df.where("age = 2").collect()
[Row(age=2, name=u'Alice')]

有关详细信息,请参阅Keyboard not shown when i click on edittextview in android?