我是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>
我在这个项目中发现了错误,原因是我错过了在manifest.xml中添加以下行
<uses-permission android:name="android.permission.INTERNET"/>
现在很好。谢谢大家的支持。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句