Add auto-focus keyboard on app open
This commit is contained in:
parent
80e07cefef
commit
96223a0f38
3 changed files with 28 additions and 7 deletions
|
|
@ -1,8 +1,10 @@
|
|||
package com.duckai.app.web
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
|
@ -18,7 +20,7 @@ class MainActivity : AppCompatActivity() {
|
|||
|
||||
webView = findViewById(R.id.webView)
|
||||
webView.settings.javaScriptEnabled = true
|
||||
webView.webViewClient = WebViewClient()
|
||||
webView.webViewClient = WebViewClientOverride()
|
||||
|
||||
val query = intent?.data?.getQueryParameter("q")
|
||||
val url = if (query != null) {
|
||||
|
|
@ -30,6 +32,22 @@ class MainActivity : AppCompatActivity() {
|
|||
webView.loadUrl(url)
|
||||
}
|
||||
|
||||
private inner class WebViewClientOverride : WebViewClient() {
|
||||
override fun onPageFinished(view: WebView?, url: String?) {
|
||||
view?.postDelayed({
|
||||
view.evaluateJavascript(
|
||||
"document.querySelector('input[type=\"text\"], input[placeholder*=\"задайте\"], input[placeholder*=\"ask\"], [role=\"combobox\"]')?.focus();"
|
||||
) { }
|
||||
showKeyboard()
|
||||
}, 500)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showKeyboard() {
|
||||
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
this.intent = intent
|
||||
|
|
@ -38,6 +56,7 @@ class MainActivity : AppCompatActivity() {
|
|||
if (query != null) {
|
||||
val url = "https://duck.ai/?q=${Uri.encode(query)}"
|
||||
webView.loadUrl(url)
|
||||
webView.postDelayed({ showKeyboard() }, 1000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,11 +2,11 @@
|
|||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/widget_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_height="48dp"
|
||||
android:background="@drawable/widget_background"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal"
|
||||
android:padding="16dp">
|
||||
android:paddingHorizontal="16dp">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="24dp"
|
||||
|
|
@ -17,9 +17,10 @@
|
|||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:text="@string/search_duckai"
|
||||
android:textColor="#757575"
|
||||
android:textSize="14sp" />
|
||||
android:layout_marginStart="12dp"
|
||||
android:text="@string/ask_ai"
|
||||
android:textColor="#1a1a1a"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -3,4 +3,5 @@
|
|||
<string name="app_name">DuckAI</string>
|
||||
<string name="search">Search</string>
|
||||
<string name="search_duckai">Search DuckAI</string>
|
||||
<string name="ask_ai">Ask AI</string>
|
||||
</resources>
|
||||
Loading…
Add table
Add a link
Reference in a new issue