[英]EditText input doesn't appear when I click in an edittextpreference
[英]The keyboard doesn't appear when I click on the EditText which is in the ListView
我創建了一個帶有ListView的Alert對話框,該ListView包含一個EditText,問題是當我單擊EditText時,鍵盤沒有出現。 請幫助我。 這是我的代碼:
主要活動:
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>
您可以將以下代碼添加到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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.