我在其中创建了一个带有ListView的Alert Dialog,ListView包含一个EditText,问题是当我点击EditText时,键盘没有显示出来。请帮助我。这是我的代码:
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;
}
}
}
这是警报对话框 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>
最后这里是包含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>
答案 0 :(得分:0)
您可以将以下代码添加到Layout xml中的EditText,
android:focusable="true"
android:focusableInTouchMode="true"
此外,您可以在代码中将其设置为
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
,或者
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(edittext, 0);