如何在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. 我怎樣才能從我的應用程序從Android遠程URL打開PDF
- 2. 我怎樣才能讓引導程序4中的按鈕浮起來而不離開它所在的容器?
- 3. 我怎樣才能打開我的Android應用程序的多個實例
- 4. 我怎樣才能讓我的Android應用程序時尚的按鈕
- 5. 我怎樣才能flex 4 DataGrid組件?
- 6. 我怎樣才能在Python打開網址,如PHP
- 7. 我們怎樣才能使一個4×4格使用CSS
- 8. 我怎樣纔能有一個網站的箭頭按鈕來打開我的主頁內的不同部分
- 9. 我怎樣才能從angular2/4運行exe安裝程序
- 10. Android:我怎樣才能讓我的應用程序多語言?
- 11. 我怎樣才能插入打印按鈕,在網頁
- 12. 怎樣才能打開多個網址,在C#中讀取
- 13. 你怎麼能按網頁上的按鈕打開Windows程序
- 14. 我怎樣才能在我的程序中獲得_GLOBAL_OFFSET_TABLE_地址?
- 15. 我怎樣才能發育過程中Asp.Net MVC 4
- 16. 我怎樣才能打破一個網址,並使用PHP
- 17. 我怎樣才能使用真正的應用程序對象與Android的ServiceTestCase
- 18. 我怎樣才能整合SQL Server和我的android應用程序與推
- 19. Android:我怎樣才能從Android應用獲取IP地址?
- 20. 按鈕不能打開,由eclipse製作的android應用程序
- 21. 我怎樣才能打開FLP文件?
- 22. 我怎樣才能使周圍按鈕
- 23. 我怎樣才能使按鈕出現
- 24. 網址來打開Android應用程序
- 25. 我怎樣才能總是讓我的應用程序響應
- 26. 我怎樣才能在Android上調用Facebook應用程序?
- 27. 我怎樣才能縮短我的網址中的Zend
- 28. 我怎樣才能獲得網址前面的與
- 29. 我怎樣才能得到一部分的網址與PHP?
- 30. 我怎樣才能改變我的應用程序的語言
創建在XML文件的WebView和點擊每個按鈕通過網站的網址,你要加載使用意圖和動態與URL – vishnumm93
可能的代碼示例加載網頁流量ShowWeb活動解釋這個普萊茲 –