Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the web page is displayed in the webView when the button is clicked. 当点击按钮时在webView显示网页。 #1071

Open
cleversheep2011 opened this issue Jan 6, 2023 · 4 comments

Comments

@cleversheep2011
Copy link

The following questions are from machine translation, please understand if there is any translation error.

Hello! Recently I have had some problems learning Android. When the following code runs, the webView control doesn't display or only shows a white screen, I want it to appear on the screen. What to do?

你好!最近我在学习Android时遇到了一些问题。以下代码运行时,webView 控件不显示或仅显示白屏,我希望它显示在屏幕上。怎么办?

====MainActivity.java====
package com.example.myapplication;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebViewClient(new HelloWebViewClient());
        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://afdian.net/a/cleversheep");
            }
        });
        Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://afdian.net/a/DYC668YSZC");
            }
        });
    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
====activity_main.xml====
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="绵羊云app"
        android:textSize="50dp"
        android:textStyle="bold"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:text="这个app还在测试 只能看看 你可以查看绵羊云工作室的官网:sheepyun.top 备用网址:yuncheng.fun"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="@+id/textView1"
        app:layout_constraintTop_toBottomOf="@+id/textView1"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sheep的爱发电"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="48dp"
        android:text="小云的爱发电"
        app:layout_constraintBottom_toBottomOf="@+id/button1"
        app:layout_constraintEnd_toEndOf="@+id/textView2" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="409dp"
        android:layout_height="529dp"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        tools:layout_editor_absoluteX="1dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
====AndroidManifest.xml====
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/icon_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

I created two buttons and asked them to display the page whenever they clicked

我创建了两个按钮,并要求它们在单击时显示页面

@cleversheep2011
Copy link
Author

I'm a primary school student, so my English is not good, I use machine translation to generate this text, if there is an error, please understand

@Eross220
Copy link

I have checked your requirements.
I can fix your problem.
Please feel free to contact me if you would like.
my skype id is live:.cid.2a415ab05458e6ac.
Best regards.

@cleversheep2011
Copy link
Author

我已经检查了您的要求。我可以解决你的问题。如果您愿意,请随时与我联系。我的Skype ID是实时的:.cid.2a415ab05458e6ac。 此致敬意。

Sorry, Skype doesn't work on my computer, so I can't contact you.If I can open it, I'll contact you!

@Yu2002s
Copy link

Yu2002s commented Mar 13, 2024

webview设置项少了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@Yu2002s @Eross220 @cleversheep2011 and others