Java Android 打开 URL onclick 某个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24261224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:11:05  来源:igfitidea点击:

Android open URL onclick certain button

javaandroideclipse

提问by Dr.Mezo

How i can send people to my google play on click certain button ? i already did this in activity_menu.xml

我如何通过单击某个按钮将人们发送到我的 google play?我已经在 activity_menu.xml 中做了这个

<Button
        android:id="@+id/ratebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/morebutton"
        android:layout_alignBottom="@+id/morebutton"
        android:layout_toLeftOf="@+id/morebutton"
        android:layout_toRightOf="@+id/aboutButton"
        android:background="@drawable/more"
        android:text="@string/rateBtn"
        android:textColor="@color/White"
        android:textColorHint="@color/White"
        android:textSize="18sp" />

but in MenuActivity.java i couldn't know what should i do ?

但是在 MenuActivity.java 中我不知道该怎么办?

Any help? Thanks

有什么帮助吗?谢谢

采纳答案by Suhail Mehta

findViewById(R.id.ratebutton).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = "market://details?id=<package_name>";

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });

回答by Kailash Gajara

Declare button inside your activity:

在您的活动中声明按钮:

Button btnRateButton;

Initialize btnRateButton inside onCreate method:

在 onCreate 方法中初始化 btnRateButton:

btnRateButton = (Button) findViewById(R.id.ratebutton);

Set on click listener:

设置点击监听器:

btnRateButton.setOnClickListener(this);

Write onclick method:

编写onclick方法:

@Override
public void onClick(View v) {
    Intent i = null;
    final String myPackage = getPackageName();
    if(v.getId() == R.id.btnRateButton) {
        i = new   Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + myPackage));
        startActivity(i);

    }
}

Note that for above code to work, you need to have Play Store app installed on your device.

请注意,要使上述代码正常工作,您需要在设备上安装 Play 商店应用。

回答by nawaab saab

Button rate = (Button)findViewById(R.id.rate);
rate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i2=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=yourpackagename"));
            startActivity(i2);
            }
        });

// Enter package name of application in place of yourpackagename

// 输入应用程序的包名代替你的包名

回答by Jaime Montoya

In my signin.xmlfile I put this code:

在我的signin.xml文件中,我放置了以下代码:

    <Button
        android:id="@+id/privacypolicylink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="50dp"
        android:background="@android:color/transparent"
        android:onClick="performSingUpMethod"
        android:text="Política de Privacidad"
        android:textColor="#A9A9A9"
        android:textSize="15dp"
        android:textStyle="bold" />

In my signin.javafile I used this:

在我的signin.java文件中,我使用了这个:

findViewById(R.id.privacypolicylink).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = "https://example.net/privacy_policy";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

It works for me. It displays a text in the grey color that I want it. The text is underlined and it is a link. The is the way it looks:

这个对我有用。它以我想要的灰色显示文本。文本带有下划线,它是一个链接。这是它的样子:

enter image description here

在此处输入图片说明

When I click on it, I see this:

当我点击它时,我看到了这个:

enter image description here

在此处输入图片说明

Then I can open the link using a web browser on the phone and everything works correctly the way I needed it.

然后我可以使用手机上的网络浏览器打开链接,一切都按我需要的方式正常工作。