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