簡體   English   中英

如何在Java中將鍵入的字符串傳遞給Google搜索

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

我是android的新手,當有人單擊搜索時在搜索視圖中鍵入字符串,然后嘗試鍵入此字符串或在Google搜索時應在網絡視圖中鍵入任何內容時,我正在嘗試。 我已經手動給出了一個字符串,它正在工作,但是我需要執行搜索。 我已在清單中添加了Internet的使用權限

這是我的代碼。

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

}

這是我的清單

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


}}

我現在不能給您完整的答案,但是也許對您考慮setText和getText函數會很有用

使用EditText和一個按鈕

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

EditText獲取文本

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM