英文:
Change webView visibility from another class
问题
以下是您要翻译的内容:
因此,我的主类中有一个 webview 元素,我想将其可见性更改为隐藏,
我所需要的就是总的来说:
myWebView.setVisibility(View.GONE);
但是我的问题是,当我想从另一个类对其进行控制时,我在互联网上搜索了很多,但找不到令人满意的答案,
以下是我尝试过的:
MassegeNetsActivity.java(主活动)
fullScreenFu.openFullScreen(MassegeNetsActivity.this);
fullScreenFu.java:
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
public class videoFullScreen extends AppCompatActivity {
public void openFullScreen(Context c) {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setVisibility(View.GONE);
}
}
但是当我调用该函数时,什么都不会发生,没有错误,但它只是不会响应,并且 webview 仍然会显示出来。
当然,这个问题也适用于图像按钮或文本,对我来说,它与 webview 一起出现了
非常感谢!
英文:
So I have a webview element in my main class, and I want to change its visibility to hidden,
All I need is a total of it:
myWebView.setVisibility(View.GONE);
But my problem is when I want to control it from another class, I searched a lot on the internet and could not find a satisfactory answer,
Here's what I tried:
MassegeNetsActivity.java: (main activity)
fullScreenFu.openFullScreen(MassegeNetsActivity.this);
fullScreenFu.java:
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
public class videoFullScreen extends AppCompatActivity {
public void openFullScreen(Context c) {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setVisibility(View.GONE);
}
}
But when I call the function nothing happens, there is no error but it just does not respond, and the webview continues to be displayed.
Of course this is a question that is also relevant to the image button or text, for me it came out with a webview
Thank you very much!
答案1
得分: 2
创建一个 WebView 作为类变量,并在你的 onCreate 方法中进行初始化,这样当你想要隐藏它时,只需调用类变量,而不是每次都通过 id 查找它。
英文:
create your webview as a class variable and initialize it in your onCreate method so that when you want to hide it, you just call the class variable instead of finding it by id every time
答案2
得分: 0
所以在使用@Wongani之后,我能够做一些很好的事情,所以应该是这样的:
第一步,我们将浏览器定义为主类中的一个类类型:
public static Webview myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
myWebView = (Webview) findViewById(R.id.myWebView);
}
第二层:
现在我们可以从第二个类中调用它,就像这样:
MainActivity.myWebView.setVisibility(View.GONE);
非常简单:)
祝好运!
英文:
So after using @Wongani I was able to do something that would work well, so it should be like this:
Step One We will define the browser as a class type in main class:
public static Webview myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
myWebView = (Webview) findViewById(R.id.myWebView);
}
second level:
We can now call him from the second class, like this:
MainActivity.myWebView.setVisibility(View.GONE);
it's very simply:)
good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论