[英]Partially clickable TextView and different text colors for the text -Android
我的xml代码是;
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginTop="25dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center">
<TextView
android:id="@+id/signup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#733E3A"
android:textSize="15dp"
android:text="@string/have_account"
fontPath="fonts/pfdindisplaypro-regular.ttf"
tools:ignore="MissingPrefix"
android:layout_gravity="center_horizontal" />
<TextView
android:id="@+id/txt_login_go_to_register"
android:onClick="loginOnClickListener"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/signup1"
android:textColor="@color/white"
android:layout_marginLeft="7dp"
android:textSize="15dp"
android:text="@string/txt_register_title"
fontPath="fonts/pfdindisplaypro-regular.ttf"
tools:ignore="MissingPrefix"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
我用过2 TextView。 其中之一是静态的,但第二个是动态的。 当用户单击第二个TextView时,片段将进入抵抗页面。
您可以使用Spannable实现
TextView布局。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:clickable="true"
android:background="#BF7366" //Use your Background image or any other color
android:padding="5dp"
/>
在你的activity.java中
TextView textView =(TextView) findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Spannable word = new SpannableString("Don`t have account yet? ");
word.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.text_color)), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(word);
final Spannable wordTwo = new SpannableString("Sign up here and join our exciting team.");
ClickableSpan myClickableSpan = new ClickableSpan()
{
@Override
public void onClick(View widget) {
// There is the OnCLick. put your intent to Register class here
widget.invalidate();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);
}
};
wordTwo.setSpan(myClickableSpan,0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(wordTwo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.