我是android新手,我正在尝试当有人在搜索 View 中输入字符串时单击搜索,然后输入此字符串或在谷歌搜索的 WebView 中应输入的任何内容。 我已经手动给出了一个字符串,它正在工作,但我需要实现搜索。我在 list 中添加了互联网的使用权限
这是我的代码。
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/webView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22000003">
<EditText
android:id="@+id/search_for"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType=""
android:autofillHints="" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</SearchView>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.47000003" />
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edit = (EditText) findViewById(R.id.search_for);
final Button searchBtn = (Button) findViewById(R.id.myButton);
final String searchText = edit.getText().toString();
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("http://www.google.com/search?q=" + searchText);
}
});
}
}
这是我的 list
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhuniqia.searchwithus">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
Xml
<LinearLayout
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"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<EditText
android:id="@+id/search_for"
android:imeOptions="actionSearch"
android:inputType="text"
android:layout_width="match_parent"
android:layout_toLeftOf="@id/myButton"
android:layout_height="wrap_content"
android:hint="Search" />
<Button
android:id="@+id/myButton"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_width="60dp"
android:text="Go"
/>
</RelativeLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/></LinearLayout>
Java 类
public class MainActivity extends AppCompatActivity {
public WebView myWeb;
public Button myBtn;
public EditText myEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWeb = findViewById(R.id.webView);
myBtn = findViewById(R.id.myButton);
myEdit = findViewById(R.id.search_for);
myEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
final String myText = myEdit.getText().toString();
myWeb.loadUrl("http://www.google.com/search?q=" + myText);
return true;
}
return false;
}
});
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String myText = myEdit.getText().toString();
myWeb.loadUrl("http://www.google.com/search?q=" + myText);
}
});
myWeb.getSettings().setJavaScriptEnabled(true);
myWeb.setWebChromeClient(new WebChromeClient());
myWeb.setWebViewClient(new WebViewClient());
myWeb.loadUrl("https://www.google.com");
}}
关于java - 如何在java中将输入的字符串传递给谷歌搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53072763/