美文网首页
Android Dialog 底部输入框 弹出输入法

Android Dialog 底部输入框 弹出输入法

作者: 菜鸟考官 | 来源:发表于2019-06-14 11:01 被阅读0次
image.png
image.png

方法一(推荐):
通过设置Dialog主题

   //复写构造方法
 constructor(context: Context, themeResId: Int) : super(context,themeResId) {
    this.mContext = context
    }
 editCommentDialog = EditCommentSDialog(this,R.style.dialog_soft_inputs)

方法二(不推荐):

    //延时弹出输入法
   //获取焦点
    editComment.requestFocus()
    editComment.post(object :Runnable{
          override fun run() {
     showSoftInput(mContext)
           val inputManager =
                editComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as 
  InputMethodManager
            inputManager.showSoftInput(editComment, 0)
        }

方法二的类:

  /**
 * @author azheng
 * @date 2019/6/3.
* Description:评论
  */
class EditCommentDialog : Dialog {

private lateinit var onButtonListener: OnButtonListener
private var mContext: Context


constructor(context: Context, themeResId: Int) : super(context,themeResId) {
    this.mContext = context
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.dialog_edit_comment)

    val window = window!!
    val layoutParams = window.getAttributes()
    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
    layoutParams.horizontalMargin = 0f
    window.setAttributes(layoutParams)
    window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    window.getDecorView().setPadding(0, 0, 0, 0)
    window.setGravity(Gravity.BOTTOM)

    var editComment = findViewById<EditText>(R.id.editCommentDialog)
    val tvSend = findViewById<TextView>(R.id.tvSendCommentDialog)
    tvSend.onSingleClick {
        if (onButtonListener != null) {
            onButtonListener.onSendDialog(editComment.text.toString())
        }
    }
    editComment.requestFocus()
    editComment.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
            showSoftInput(mContext)
            return false
        }
    })
}

fun setListener(onButtonListener: OnButtonListener): EditCommentDialog {
    this.onButtonListener = onButtonListener
    return this
}

interface OnButtonListener {

    fun onSendDialog(editsString: String)

}
/**
 * 显示软键盘,Dialog使用
 *
 * @param activity 当前Activity
 */
fun showSoftInput(mContext: Context) {
    val inputMethodManager = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}

}

XLM

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:orientation="horizontal"
          android:background="@color/common_white"
          android:layout_width="match_parent"
          android:paddingStart="@dimen/common_padding"
          android:paddingEnd="@dimen/common_padding"
          android:paddingBottom="@dimen/space_8dp"
          android:paddingTop="@dimen/space_8dp"
          android:layout_height="wrap_content">

<EditText
        android:id="@+id/editCommentDialog"
        android:hint="我也说一句…"
        android:background="@color/common_gray"
        android:textSize="@dimen/text_14sp"
        android:maxLines="1"
        android:singleLine="true"
        android:lines="1"
        android:inputType="text"
        android:paddingStart="@dimen/space_8dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="48dp"/>

<TextView
        android:id="@+id/tvSendCommentDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:gravity="center"
        android:layout_marginStart="@dimen/space_8dp"
        android:textColor="@color/common_red"
        android:textSize="@dimen/text_16sp"
        />
 </LinearLayout>

主题

 <style name="dialog_soft_input" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:windowSoftInputMode">stateVisible|adjustPan</item>
</style>

弹出输入法方法:

 /**
 * 显示软键盘,Dialog使用
 *
 * @param activity 当前Activity
 */
 fun showSoftInput(activity: Context) {
    val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}

/**
 * 隐藏软键盘
 *
 * @param activity 当前Activity
 */
fun hideSoftInput(activity: Activity) {
    val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(
        activity.window.decorView.windowToken, 0
    )
}

相关文章

网友评论

      本文标题:Android Dialog 底部输入框 弹出输入法

      本文链接:https://www.haomeiwen.com/subject/bfrufctx.html