android两个按钮连接到网页链接
我已经有一段时间了(3天)。我正在尝试使用Eclipse制作一个android应用程序。我想要两个图像按钮。每一个链接到不同的站点。我没能做到。我已经能够使用webview用一个按钮打开一个网页,但不是两个。我转而尝试使用意图,因为我在某个地方读到这是更好的方法。最后,我想做的是在应用程序中打开页面,并使用“后退”按钮返回应用程序的主屏幕,查看每个按钮/页面。这是到目前为止我的代码 MainActivity.javaandroid两个按钮连接到网页链接,android,eclipse,button,android-intent,Android,Eclipse,Button,Android Intent,我已经有一段时间了(3天)。我正在尝试使用Eclipse制作一个android应用程序。我想要两个图像按钮。每一个链接到不同的站点。我没能做到。我已经能够使用webview用一个按钮打开一个网页,但不是两个。我转而尝试使用意图,因为我在某个地方读到这是更好的方法。最后,我想做的是在应用程序中打开页面,并使用“后退”按钮返回应用程序的主屏幕,查看每个按钮/页面。这是到目前为止我的代码 MainActivity.java package com.modsbyus.onoff; import
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>
你们能提供的任何帮助都会很好。
谢谢 我会将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 on imagebuttons在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属性android:onClick=“methodName”,您需要声明一个公共方法,该方法返回void并接受一个视图作为参数,其名称与
onClick=“methodName”
中定义的名称相同
代码中需要的只是将参数视图添加到方法Light和Light1。因此,改变:
public void Light()
public void Light1()
致:
而且,正如Rick所建议的,将android.onClick=更改为android:onClick=好的,我做了所有的更正。我用了里克斯的建议。看起来很干净。现在,在我的布局中,两个按钮都有android:onClick=“onClick();”。在我的主要活动中,我有Rick说要做的,它编译得很好,但当你试图运行它时,force关闭了。有什么建议吗?没有罗格特,我是在黑暗中拍摄的。我在清单顶部添加了您需要的权限。但是你需要发布你的日志这些权限在清单中的什么地方?在uses sdk下?你可以查看我的日志猫,它不会让我在这里发布它。它试图调用Light1();看法确保xml文件中没有onClick=“Light1”这两个名称都应该是onClick=“onClick”,并且位于应用程序标记的正上方
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);
}
}
public void Light()
public void Light1()
public void Light(View v)
public void Light1(View v)