Create clickable link in text view in android

后端 未结 2 754
Happy的楠姐
Happy的楠姐 2020-11-29 09:55

I have some hash tags in a TextView which are starts with \'#\'

Example:\"#one#two Hello World #three\".

I wants these hash tags clickable separ

2条回答
  •  独厮守ぢ
    2020-11-29 10:40

    You can use LinearLayout :

    • In xml add :

      
      
      

      
      

    • Init layout :

      // EditText to write Tags.
      EditText tag = (EditText) findViewById(R.id.tag_ET));
      // Button used to add tags
      Button addTagBtn = (Button) findViewById(R.id.add_Btn));
      // layout contains added tags.
      final LinearLayout tagsView = (LinearLayout) findViewById(R.id.tags_view);
      
    • Add tags to tagView :

          final String tagTxt = tagEt.getText().toString();
          if (!tagTxt.isEmpty()) {
              Spannable buttonLabel = new SpannableString(tagTxt);
      
              // clear tag edit text
              tagEt.setText("");
      
              // add tag to layout
              Button btn = new Button(this);
              LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              lout.addView(btn, tagsView);
      
              btn.setText(buttonLabel);
              btn.setBackgroundResource(R.drawable.hover);
              btn.setOnClickListener(new OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      // Toast whatever you want.
                      Toast.makeText(getApplicationContext(), tagTxt,1000).show();
                  }
              });
          }
      

提交回复
热议问题