【问题标题】:android Two buttons to webpage linksandroid 网页链接的两个按钮
【发布时间】:2013-12-19 21:16:36
【问题描述】:

我在这方面已经有一段时间了(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>

你们能提供的任何帮助都会很棒。 谢谢!

【问题讨论】:

    标签: android eclipse button android-intent


    【解决方案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);
     }
    }
    

    【讨论】:

    • 好的,我做了所有的更正。我使用了里克斯的建议。那看起来很干净。所以现在我的布局中有 android:onClick="onClick();"两个按钮。在我的 MainActivity 中,我有 Rick 说要做的事情,它编译得很好,但是当你尝试运行它时会强制关闭。有什么建议吗?
    • 没有logcat,我在黑暗中拍摄。我在清单顶部添加了您需要的权限。但是你需要发布你的 logcat
    • 这些权限在清单中的什么位置?在uses-sdk下?
    • 你可以查看我的日志猫cloud.modsbyus.com/…它不会让我在这里发布。
    • 它的窗台试图调用 Light1();View。确保您的 xml 文件中的任何位置都没有 onClick="Light1" 两者都应该说 onClick="onClick" 并且在您的应用程序标记的正上方以获得权限
    【解决方案2】:

    为了使用 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=

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2017-08-31
      • 2014-08-05
      相关资源
      最近更新 更多