我用android studio编了一个小的APP,界面上有textView5, textView7, button5, 现在想用代码增加以下功能。
请帮我写一个实现上面功能的代码。(页面部署,按钮的监听等代码都不用,我的APP都可以正确使用。)
如果要用到特殊的类或包,请一并说明怎么弄。
不要讲原理,不要发一个链接给我,直接帮我写出代码,越简单越好。(如果你仅是发我一个其他地方代码的链接,就不要回答我这个问题了。谢谢!)
MainActivity的代码:
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView textView5,textView7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView5.setText("今天天气真好");
final SpannableStringBuilder style = new SpannableStringBuilder();
//设置文字
style.append("今天天气真好");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
textView7.setText("成功实现再跳转");
}
};
style.setSpan(clickableSpan, 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView5.setText(style);
//设置部分文字颜色
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
style.setSpan(foregroundColorSpan, 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//配置给TextView
textView5.setMovementMethod(LinkMovementMethod.getInstance());
textView5.setText(style);
}
});
}
public void init(){
btn=findViewById(R.id.button5);
textView5=findViewById(R.id.textView5);
textView7=findViewById(R.id.textView7);
}
}
activity_main.xml的代码:
```<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textView5"
android:gravity="center"
android:textSize="24dp"/>
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="textView7"
android:gravity="center"
android:textSize="24dp"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button5"
/>
</LinearLayout>
```