Android Studio Add a picture with control TextView

Source: Internet
Author: User

Job: Add a picture with TextView: Create a button, press the button to switch a picture, a total of 5 pictures

1. Add a picture to the resource folder

2. Implement the following code

 Public classMainactivityextendsappcompatactivity {TextView tx;    Button bt1; intA;    Imagespan Span1,span2,span3,span4,span5; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TX=(TextView) Findviewbyid (R.ID.TEXTVIEW1); BT1=(Button) Findviewbyid (R.id.button); A= 1; Span1=NewImagespan ( This, R.MIPMAP.DANGAO1); Span2=NewImagespan ( This, R.mipmap.daogao2); Span3=NewImagespan ( This, R.mipmap.dangao3); Span4=NewImagespan ( This, R.mipmap.dangao4); Span5=NewImagespan ( This, R.MIPMAP.DANGAO5); Spannablestring Spanstr=NewSpannablestring (""); Spanstr.setspan (Span1, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);        Tx.settext (SPANSTR); A++; Bt1.setonclicklistener (listener1);//Create a listener for BT1}} button.onclicklistener listener1=NewButton.onclicklistener () {//Creating Listener Objects         Public voidOnClick (View v) {//what needs to be done in the listener .           if(a==2) {spannablestring spanstr=NewSpannablestring (""); Spanstr.setspan (Span2, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);                Tx.settext (SPANSTR); A++; }            Else if(a==3) {spannablestring spanstr=NewSpannablestring (""); Spanstr.setspan (Span3, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);                Tx.settext (SPANSTR); A++; }            Else if(a==4) {spannablestring spanstr=NewSpannablestring (""); Spanstr.setspan (Span4, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);                Tx.settext (SPANSTR); A++; }            Else if(a==5) {spannablestring spanstr=NewSpannablestring (""); Spanstr.setspan (Span5, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);                Tx.settext (SPANSTR); A++; }            Else if(a==6) {spannablestring spanstr=NewSpannablestring (""); Spanstr.setspan (Span1, spanstr.length ()-1, Spanstr.length (), spannable.span_inclusive_exclusive);                Tx.settext (SPANSTR); A=2; }        }    };}

Android Studio Add a picture with control TextView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.