“在Android Studio中打开链接的按钮”

huangapple go评论75阅读模式
英文:

Button to open link in Android Studio

问题

我对Java和Kotlin都很陌生,基本上对Android Studio也不熟悉。

我尝试制作一个简单的应用,当点击时会跳转到一个链接。 就像在Android Studio中,选择了一个空的活动和Java语言。在设计中,只放置一个居中的按钮,当按下时会打开一个指向YouTube的链接。很简单。

我尝试了很多我在网上找到的代码,但总是出现了很多错误,无法构建APK。

有人可以告诉我怎么做吗,可以给我一些代码吗,以便我放到xml和主活动中? 我会非常感激。如果我表达得不够清楚,我很抱歉 “在Android Studio中打开链接的按钮”

编辑:我指的是可以从这里复制并粘贴到xml和主活动中的代码,抱歉,我只是在尝试,并且我对stackoverflow也很陌生!

英文:

I'm really new to Java and Kotlin, basically also new to Android Studio.

I was trying to make a simple app which when clicked on, takes to a link. Like in Android studio, an empty activity and Java language chosen. In the design just a button in middle, and when pressed a link to YouTube opens. Simple.

I used countless codes I found online but always had several errors in the code or building the APK.

Can someone tell me how do I make it, give me the code maybe to put in xml and main activity? I'd be really thankful. I'm sorry if I wasn't clear enough “在Android Studio中打开链接的按钮”

EDIT: By code I mean, something I can copy from here and just paste in the xml and main activity, sorry I'm just experimenting and I'm new to stack overflow too!

答案1

得分: 0

我为您制作了一个示例应用。只需克隆存储库或下载它,导入到Android Studio并构建它。

存储库链接:https://github.com/Farbklex/openUrlExample

我保持它相对简单。activity_main.xml 文件包含布局。它使用 ConstraintLayout 来排列元素。
文本输入由 TextInputLayoutTextInputEditText 处理,它们来自于 Material Design 库。这些组件用于美化界面。

MainActivity 查找 EditTextButton,并将它们与逻辑连接起来。
当点击按钮时,将调用 onOpenUrlButtonClicked 方法。然后,它会检查输入的 EditText,并尝试将其解析为 Uri。如果可以解析,它会尝试在设备上查找可以处理该 Uri 的活动。

如果无法解析 Uri 或设备上找不到匹配的活动,则显示错误消息。

private void onOpenUrlButtonClicked() {
    String inputString = urlInput.getText().toString();
    Uri parsedUrl = null;
    try {
        parsedUrl = Uri.parse(inputString);
    } catch (Exception ex) {
        Log.e(TAG, "无法将输入解析为 URL。输入:" + inputString);
        Toast.makeText(this, "无法解析 URL", Toast.LENGTH_LONG).show();
    }

    if (parsedUrl != null) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(parsedUrl);
            startActivity(intent);
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(this, "无法打开 URL。您是否输入了有效的 URL(https://...)?", Toast.LENGTH_LONG).show();
        }
    }
}

“在Android Studio中打开链接的按钮”

英文:

I made an example app for you. Just clone the repository or download it, import into android studio and build it.

https://github.com/Farbklex/openUrlExample

I kept it fairly simple. The activity_main.xml file contains the layout. It uses a ConstraintLayout to arrange the elements.
The text input is handled by the TextInputLayout and TextInputEditText which are from the Material Design library. Those are used to make it look a little better.

The MainActivity looks for the EditText and Button and wires them up with logic.
When the Button is clicked, the onOpenUrlButtonClicked method is called. It then checks the EditText for input and tries to parse it to a Uri. If it can be parsed, it then tries to find an activity on the device which can handle the Uri.

If the Uri can't be parsed or no matching activity has been found on the device, an error message is shown.

private void onOpenUrlButtonClicked() {
    String inputString = urlInput.getText().toString();
    Uri parsedUrl = null;
    try {
        parsedUrl = Uri.parse(inputString);
    }catch (Exception ex){
        Log.e(TAG, "Failed to parse input to a URL. Input: " + inputString);
        Toast.makeText(this, "Failed to parse URL", Toast.LENGTH_LONG).show();
    }

    if(parsedUrl != null){
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(parsedUrl);
            startActivity(intent);
        }catch (ActivityNotFoundException ex){
            Toast.makeText(this, "Can't open URL. Did you enter a valid URL (https://...)", Toast.LENGTH_LONG).show();
        }

    }
}

“在Android Studio中打开链接的按钮”

huangapple
  • 本文由 发表于 2020年9月28日 23:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64104706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定