在我的Kotlin android项目(完成后应该是ToDo List应用程序)中,我在弹出的窗口中添加了editText。起初,键盘甚至没有打开。然后我将这段代码添加到Main Activity(称为ToDo)中:
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as
InputMethodManager
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
现在,软键盘打开,但是当我键入内容时,没有文本输入到editText中。我尝试添加requestFocus 到弹出式xml中的editText,这没用。
我在模拟器和真实电话上都尝试过。此处针对类似问题的其他解决方案均无效。回复时,请记住,我需要Kotlin中的解决方案。请帮助我使editText起作用。谢谢。这是我的活动代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_to_do)
setSupportActionBar(toolbar)
fab.setOnClickListener {
val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.todopopup, null)
val popupWindow = PopupWindow(
view,
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT)
TransitionManager.beginDelayedTransition(coordinatorLayout)
popupWindow.showAtLocation(
coordinatorLayout,
Gravity.CENTER,
0,
0
)
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
val cancelbutton = view.findViewById<Button>(R.id.cancelbutton)
cancelbutton.setOnClickListener {
popupWindow.dismiss()
}
}
}
这是我的活动布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ToDo">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:visibility="visible"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="@+id/include"
layout="@layout/content_to_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:elevation="4dp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>
这是我的弹出窗口xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
android:elevation="16dp"
>
<EditText
android:id="@+id/editText"
android:layout_width="384dp"
android:layout_height="46dp"
android:inputType="text"
android:textColor="#000000"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="8dp"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toBottomOf="@+id/textView"
/>
<Button
android:id="@+id/addtodobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add ToDo"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
android:background="#FFFFFF"/>
<Button
android:id="@+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel"
app:layout_constraintTop_toBottomOf="@+id/editText"
app:layout_constraintEnd_toStartOf="@id/addtodobutton"
android:background="#FFFFFF"
/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:text=" Add a ToDo"
android:textColor="#000000"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我是android开发的新手,所以请以一种易于理解的方式解释您的答案。我只希望editText工作。问题可能与editText在弹出窗口中有关吗?我希望代码在这里正确显示。感谢您提前答复!
答案 0 :(得分:0)
在FAB的click listener块的末尾添加这两行。
popupWindow.isFocusable = true
popupWindow.update() // This line updates the state of the popup window.