【问题标题】:How to create a Link button programmatically?如何以编程方式创建链接按钮?
【发布时间】:2018-06-14 12:05:17
【问题描述】:

我正在尝试将一系列将在浏览器中打开网页的按钮从 xml 布局文件转换为我的 Activity 类(以编程方式)。我怎样才能做到这一点?

我找不到可以回答这个问题的问题。

【问题讨论】:

    标签: android user-interface button


    【解决方案1】:
      /**
         * Open the specified URL on the device's browser
         *
         * @param context Current Context.
         * @param url     The url to display.
         */
        public static void openWebPage(@NonNull Context context, String url) {
            Uri webPage = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
            if (intent.resolveActivity(context.getPackageManager()) != null) {
                context.startActivity(intent);
            }
        }
    

    然后在您的 Activity 的 onCreate 中,您将获得按钮:

    View button = findViewById(R.id.my_button);
    button.setOnClickListener(new onClickListener(){
       ... // call the above method with the corresponding url
    });
    

    更多信息here

    【讨论】:

      【解决方案2】:

      代码....

      public class MainActivity extends Activity {
      
       TextView textView;
       Spanned spannedText;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
      
       textView = (TextView)findViewById(R.id.textView1);
      
       spannedText = Html.fromHtml("text with a link in it <br />" +
       "<a href='https://www.aol.com//'>aol.com</a>");
      
       textView.setMovementMethod(LinkMovementMethod.getInstance());
       textView.setText(spannedText);
       }
      }
      

      XML 文本视图

       <TextView
       android:id="@+id/textView1"
       android:layout_width="match_content"
       android:layout_height="match_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="LINK WILL SHOW UP HERE"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:gravity="center" />
      

      【讨论】:

        【解决方案3】:

        您需要在您的 android 应用程序中使用 webview。 More about web view

        【讨论】:

        • 不,你不知道。您可以通过代码来完成。正确答案很快就会出现。
        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        相关资源
        最近更新 更多