简体   繁体   English

如何在Java中将键入的字符串传递给Google搜索

[英]How to pass the typed string to google search in java

I am new at android and I am trying when someone type a string in search view when click search then this typed string or whatever should in web view at the google search for this. 我是android的新手,当有人单击搜索时在搜索视图中键入字符串,然后尝试键入此字符串或在Google搜索时应在网络视图中键入任何内容时,我正在尝试。 I have manually gave a string and it is working but I need to implement a search. 我已经手动给出了一个字符串,它正在工作,但是我需要执行搜索。 I have added uses permission for Internet in Manifest 我已在清单中添加了Internet的使用权限

This is my code. 这是我的代码。

<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);
            }
        });
    }

}

This is my Manifest 这是我的清单

<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 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 Class 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");


}}

我现在不能给您完整的答案,但是也许对您考虑setText和getText函数会很有用

Use EditText and a Button 使用EditText和一个按钮

final EditText edit =  (EditText) findViewById(R.id.text_xyz);
final Button searchBtn = (Button) findViewById(R.id.myBuuton);

get text from EditText EditText获取文本

String searchtext = edit.getText().toString();

Set Button to search the text you entered 设置按钮以搜索您输入的文本

searchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            webView.loadUrl("http://www.google.com/search?q=" + searchtext);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM