android两个按钮到网页链接

时间:2013-12-19 21:16:37

标签: android eclipse button android-intent

我已经有一段时间了(3天)。我正在尝试使用Eclipse制作一个Android应用程序。我想要两个图像按钮。每个链接到不同的站点。我无法做到。我已经能够使用webview打开一个带有按钮的网页,但不是两个。我已经开始尝试使用Intent,因为我在某处读到了更好的方法。最终,我想要做的是在应用程序中打开页面,然后使用后退按钮返回每个按钮/页面的应用程序主屏幕。到目前为止,这是我的代码。

MainActivity.java

    package com.modsbyus.onoff;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;



public class MainActivity extends Activity {


@Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);


      }
      public void Light()
     {

                Intent intent = new Intent(Intent.ACTION_VIEW,           uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
                startActivity(intent);
            }
    public void Light1()
    {

                Intent intent = new Intent(Intent.ACTION_VIEW,     Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
                startActivity(intent);
            }
}

和我的布局 activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.onClick="Light1"
    android:clickable="true"
    android:src="@drawable/ic_launcheronswitch" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android.onClick="Light"
    android:clickable="true"
    android:src="@drawable/ic_launcheroffswitch" />

</LinearLayout>

你们给予的任何帮助都会很棒。 谢谢!

2 个答案:

答案 0 :(得分:1)

我会将你的xml中的onclick改为android:onClick =“onClick”两个按钮,并在那里调用你的方法。只是为了看起来。确保您的类实现了OnClickListener。

然后你的onClick方法将是:

@Override
    public void onClick(View v) {
        Intent iExp = null;
        switch (v.getId()) {
        case R.id.imageButton1:
            iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
            break;
        case R.id.imageButton2:
            iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
            break;
    }
        startActivity(iExp);
    }

PS onClick图像按钮直到1.6才可用,而xml中的onClick有一个。什么时候应该是:

确保你的清单有:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

你的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:clickable="true"
    android:src="@drawable/ic_launcheronswitch" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClick"
    android:clickable="true"
    android:src="@drawable/ic_launcheroffswitch" />

</LinearLayout>

你的班级:

package com.modsbyus.onoff;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
 public void onClick(View v) {
     Intent iExp = null;
     switch (v.getId()) {
     case R.id.imageButton1:
         iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=1"));
         break;
     case R.id.imageButton2:
         iExp = new Intent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://agent.electricimp.com/BGSBpog28J0u?led=0"));
         break;

     }
startActivity(iExp);
 }
}

答案 1 :(得分:0)

为了使用xml属性android:onClick="methodName",您需要声明一个返回void的公共方法,并接受一个View作为参数,其名称与onClick="methodName"中定义的相同。

您在代码中需要的只是将参数View添加到Light和Light1方法中 所以,改变:

public void Light()
public void Light1()  

到:

public void Light(View v)
public void Light1(View v)  

并且,当 Rick 消化时,将android.onClick=更改为android:onClick=

相关问题