【发布时间】:2018-05-31 18:25:15
【问题描述】:
我对 Android 开发非常陌生,通过一些教程我使用 Eclipse 开发 Android 代码。在这里,我尝试在单击按钮后加载带有某个 URL 的 WebView,然后我想隐藏该按钮。
我正在使用线性布局。为此,我使用以下代码使其工作,但它没有加载 WebView。
public class MainActivity extends ActionBarActivity {
EditText edit;
TextView text;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
text = (TextView) findViewById(R.id.textView3);
webView = (WebView) findViewById(R.id.webView1);
Button b = (Button)findViewById(R.id.button_show);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = edit.getText().toString();
text.append(name);
webView.loadUrl("http://www.google.com");
System.out.println("Loaded Successfully--");
}
});
}
在控制台中打印消息“加载成功--”,但 WebView 不可用。
我也试过下面的代码,
EditText edit;
TextView text;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.editText);
text = (TextView) findViewById(R.id.textView3);
webView = (WebView) findViewById(R.id.webView1);
Button b = (Button)findViewById(R.id.button_show);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = edit.getText().toString();
text.append(name);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
webView.loadUrl("http://www.google.com");
System.out.println("Loaded Successfully--");
return true;
}
});
}
});
}
这次“加载成功--”没有打印出来。 我在这里错过了什么?
编辑
我的 XML 文件 (activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
tools:context="com.example.asdf.MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/back" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/imageView1"
android:layout_marginBottom="24dp"
android:ems="10"
android:hint="@string/edit_hint"
android:textColor="#ffffff"
android:textSize="20dip" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText"
android:layout_alignRight="@+id/imageView1"
android:text="@string/button_title" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/button_show"
android:text="@string/edit_hint"
android:textColor="#ffffff"
android:textSize="20dip" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="As we know the relevant data has been wide-spreaded across various sites under many intentions, factualnote is a type of social software tool in which factual data are brought forward or narrow down to the web users."
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:gravity="center"
android:text="Factualnote is an web annotation application, which helps the users to mark the specific text, element, page, video, etc in a web page and share it to like-minded people."
android:textColor="#ffffff"
android:textSize="20dip"
android:textStyle="bold" />
</RelativeLayout>
【问题讨论】:
-
按照提供的答案。