【问题标题】:How do I redirect a button to a URL or website(browser) Android? [duplicate]如何将按钮重定向到 URL 或网站(浏览器)Android? [复制]
【发布时间】:2012-09-22 08:43:50
【问题描述】:

可能重复:
How do I launch a URL from my application on Android?

我似乎无法弄清楚如何处理这个问题。我需要单击将重定向到 URL / 网站的按钮。谢谢。

【问题讨论】:

    标签: android url redirect browser


    【解决方案1】:
        button.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
            Intent intent = new Intent(Intent.ACTION_VIEW, 
                 Uri.parse("http://www.google.com"));
            startActivity(intent);
    
        }
    
    });
    

    【讨论】:

    • 非常感谢,但每当我点击按钮时,它都不会显示在网站上。 (谷歌网站)。只是停留在同一页面上。 :(
    【解决方案2】:
    load_url.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
     Uri uri = Uri.parse("http://www.google.com");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
    
    
                }
            });
    

    更新:

    ma​​nifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.testtone"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="4"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    MainActivity.java

    public class MainActivity extends Activity {
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button mButton = (Button) findViewById(R.id.button1);
            mButton.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    Uri uri = Uri.parse("http://www.google.com");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
    
    
                }
    
            });
        }
    
    
    }
    

    【讨论】:

    • @XiangCorneliaSorceress:不客气
    • 我已经尝试实现你所提供的。而且,它不会重定向到 URL。 :( 我需要对这些做其他事情吗?还是在清单中?
    • @XiangCorneliaSorceress:如果没有,您是否在清单中授予了此权限,请添加它
    • 我已经做到了。但是指导站点/ URL (google.com) 没有出现在我的模拟器中。 :(
    • @XiangCorneliaSorceress:对我来说很好用...如果您在 Logcat 中收到任何错误消息
    【解决方案3】:
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class GoogleActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Button english = (Button)findViewById(R.id.google);//button name in xml file
            english.setOnClickListener(google); // on button click listener 
        }
    
        private Button.OnClickListener google
        = new Button.OnClickListener(){
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                Uri uri = Uri.parse("http://www.google.com");
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);
    
    
                }
    
            };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-05
      • 2020-03-13
      • 2012-12-19
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2013-04-27
      相关资源
      最近更新 更多