如何在Android应用程序中使用4个按钮打开4个不同的URL? 例如,button_1打开facebook.com,button_2打开google.com,button_3打开wwe.com和button_4打开cnn.com。我想在“MainActivity”中的“ShowWeb”类中进行Web视图。我怎样才能打开4与Android应用程序中的4按钮不同的网址?
-1
A
回答
1
示例程序。您必须为每个按钮点击添加其余的代码。
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1,button2,button3,button4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent next = new Intent(MainActivity.this,NextActivity.class);
next.putExtra("url","www.facebook.com");
startActivity(next);
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="in.invis.ktpms.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="96dp"
android:layout_marginTop="29dp"
android:id="@+id/button" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_marginTop="41dp"
android:id="@+id/button2" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/button2"
android:id="@+id/button3" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_marginTop="52dp"
android:id="@+id/button4" />
</RelativeLayout>
NextActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
String Url = getIntent().getStringExtra("url");
WebView web = (WebView)findViewById(R.id.webview);
web.loadUrl(Url);
}
}
activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_next"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="in.invis.ktpms.NextActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
+0
谢谢vishnumm93它是好的 –
+0
@AsmaaMohamed永远欢迎 – vishnumm93
相关问题
- 1. 我怎样才能让引导程序4中的按钮浮起来而不离开它所在的容器?
- 2. 我怎样才能flex 4 DataGrid组件?
- 3. 我怎样才能在Python打开网址,如PHP
- 4. 我们怎样才能使一个4×4格使用CSS
- 5. 我怎样才能有一个网站的箭头按钮来打开我的主页内的不同部分
- 6. 我怎样才能从angular2/4运行exe安装程序
- 7. Android:我怎样才能让我的应用程序多语言?
- 8. 怎样才能打开多个网址,在C#中读取
- 9. 你怎么能按网页上的按钮打开Windows程序
- 10. 我怎样才能在我的程序中获得_GLOBAL_OFFSET_TABLE_地址?
- 11. 我怎样才能打破一个网址,并使用PHP
- 12. Android:我怎样才能从Android应用获取IP地址?
- 13. 我怎样才能使周围按钮
- 14. 网址来打开Android应用程序
- 15. 我怎样才能缩短我的网址中的Zend
- 16. 我怎样才能获得网址前面的与
- 17. 我怎样才能分配应用程序打开邮件附件
- 18. 我怎样才能整合iPhone应用程序中的代码
- 19. 我怎样才能在WPF中的多个按钮显示不同的背景
- 20. 我怎样才能在我的应用程序散列密码
- 21. 我怎样才能格式的地址
- 22. 我怎样才能在android
- 23. 我怎样才能把我的Android应用程序的快照/布局
- 24. 我怎样才能找到一个难题,用户还没有解决我的Rails 4应用程序
- 25. 我怎样才能从我的web应用程序进行android API调用?
- 26. Android模拟器,我怎样才能同时控制2个应用程序?
- 27. 我怎样才能从facebook的网址获取用户名?
- 28. 我怎样才能拖动一个图像按钮android
- 29. Android,我怎样才能让我的应用程序将要卸载
- 30. 我怎样才能风格单选按钮与图像
创建在XML文件的WebView和点击每个按钮通过网站的网址,你要加载使用意图和动态与URL – vishnumm93
可能的代码示例加载网页流量ShowWeb活动解释这个普莱兹 –