如何在Android中将图像按钮链接到Facebook页面

3

我刚接触安卓......

我设计了一个视图,其中创建了4个图像按钮

其中一个必须转到Facebook网址, 第二个必须进入 Twitter 网址, 第三个必须进入 YouTube 网址等等 第四个进入LinkedIn。

但是我不知道如何配置按钮以发送 URL 并使页面在下面显示的相同设计视图中。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/apple" >



<Button
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#2C3539"
    android:gravity="center"
    android:text="@string/apple"
    android:textColor="#FFFFFF"
    android:textSize="22sp" />

<Button
    android:id="@+id/video"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/music"
    android:background="#2C3539"
    android:text="@string/video"
    android:textColor="#FFFFFF"
    android:textColorHint="@color/black"
    android:textSize="20sp" />

<Button
    android:id="@+id/music"
    android:layout_width="160dp"
    android:layout_height="55dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="#2C3539"
    android:text="@string/music"
    android:textColor="#FFFFFF"
    android:textColorHint="@color/black"
    android:textSize="20sp" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/main"
    android:layout_marginLeft="41dp"
    android:layout_marginTop="72dp" >

</TableLayout>

<ImageButton
    android:id="@+id/twitter1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/facebook1"
    android:src="@drawable/twitter" />

<ImageButton
    android:id="@+id/facebook1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/main"
    android:src="@drawable/facebook" />

<ImageButton
    android:id="@+id/youtube1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/twitter1"
    android:src="@drawable/youtube" />

<ImageButton
    android:id="@+id/instagram1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/youtube1"
    android:src="@drawable/instagram_icon" /></RelativeLayout>

上面的代码如下,如果我点击Facebook图像,它必须填充此链接https://www.facebook.com/apple.fruitenter image description here 链接必须填充到正文中(如图所示)。
有人能写出后端代码如何使用按钮吗?
我已经创建了一个Java文件。
package com.lay.background;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;









public class AppleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apple);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }   }

有人能否解决这段代码?如果有任何疑问,请在评论中提出。


然后请告诉我在清单文件中我该怎么做 - Sandeep V
2个回答

7

对于每个按钮,您可以像这样传递具有特定URL的意图:

 Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
    startActivity(browserIntent);

例如,如果您的按钮名称为facebookButton,则在Activity中使用它的方式如下:
ImageButton facebookButton = (ImageButton)findViewById(R.id.facebook1);
  facebookButton.setOnClickListener(new View.onClickListener() 
   {  
        public void onClick(View arg0) 
          { 
               Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com/apple.fruit"));
                startActivity(browserIntent);

               }
         });

另一种方法是将您的URL加载到WebView中。
myWebView.loadUrl(http://"+tempUrl);

对于缺失的“http://”,我建议这样做:
if (!url.startsWith("http://") && !url.startsWith("https://"))
   url = "http://" + url;

在xml布局文件中添加WebView:

<WebView android:id="@+id/webview"
android:layout_width="fill_parent" android:layout_height="50dip"/>

在MainActivity中添加带有您的URL的WebView内容:
 // Enable javascript for the view
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
        }
    });

    mWebview .loadUrl("http://www.facebook.com/apple.fruit");

我应该在哪个Java文件中编写它?我需要将其包含在哪里? - Sandeep V
1
这取决于您的需求,您的应用程序应该如何导航。否则,让我们保持简单,对于MainActivity文件中的每个按钮都执行此操作。 - Pankaj
1
如果您想在同一布局中加载URL,请使用WebView。将WebView添加到xml布局文件中,并在同一布局中打开URL。 - Pankaj
如何在XML中添加?您能告诉我在哪里添加吗?我已经在上面添加了我的XML文件,在那里我创建了按钮。 - Sandeep V
Geek,它必须准确地显示在页面上……您能为我编写代码吗?我非常急需它,因为我对Android非常陌生。 - Sandeep V
显示剩余6条评论

0

试试这个。
首先,在您的布局文件中添加Webview,使其可见性为gone。
如果按钮被点击,则将Webview的可见性设置为visible,并使用Webview连接您想要连接的URL。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接