简体   繁体   English

当我单击ListView中的EditText时,键盘没有出现

[英]The keyboard doesn't appear when I click on the EditText which is in the ListView

I created an Alert Dialog with a ListView in it, the ListView contains an EditText, the problem is when i click on the EditText, the keyboard doesn't show up. 我创建了一个带有ListView的Alert对话框,该ListView包含一个EditText,问题是当我单击EditText时,键盘没有出现。 plz help me. 请帮助我。 here is my code: 这是我的代码:

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity {
View con_view;
ListView listView;
AlertDialog.Builder con_mBuilder;
MycustomAdapter mycustomAdapter;
ArrayList<ListItem> Item;
AlertDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void show_dialog(View view){
    con_view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
    listView = con_view.findViewById(R.id.listtvv);

    Item = new ArrayList<ListItem>();
    Item.add(new ListItem("", "", ""));

    mycustomAdapter = new MycustomAdapter(Item);
    listView.setAdapter(mycustomAdapter);
    con_mBuilder = new AlertDialog.Builder(MainActivity.this);
    con_mBuilder.setView(con_view);
    dialog = con_mBuilder.create();
    dialog.show();
}


class MycustomAdapter extends BaseAdapter {
    ArrayList<ListItem> Item = new ArrayList<>();
    public MycustomAdapter(ArrayList Item) {
        this.Item = Item;
    }

    @Override
    public int getCount() {
        return Item.size();
    }

    @Override
    public String getItem(int i) {
        return Item.get(i).name;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater linflater = getLayoutInflater();
        View view1 = linflater.inflate(R.layout.row, null);
        return view1;
    }
}

} }

and here is the Alert Dialog xml file: 这是“ 警报对话框” xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">

<ListView
    android:id="@+id/listtvv"
    android:layout_width="230dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout> 

and finally here's the xml file for the view that contains the EditText 最后是包含EditText的视图的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
tools:layout_editor_absoluteY="81dp"
>

<EditText
    android:id="@+id/bomb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:selectAllOnFocus="true"
    android:text="Name"
    android:textColor="@android:color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

You can add folowing code to the EditText in the Layout xml, 您可以将以下代码添加到Layout xml中的EditText中,

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

Also, you can set it in the code as, 另外,您可以在代码中将其设置为

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

or, 要么,

 InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 im.showSoftInput(edittext, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM