Android : Draw TextEdit(s) or TextView(s) when user enter number of them.[exp: 6 then draw 6 TextView]

自作多情 提交于 2019-12-13 07:45:28

问题


Wantto ask user to enter number of TextEdit or ViewText and then draw them. For-example user enter 7,then seven ViewText draw.I knew this is a suitable way but in my prgrm i cant change the whole structure,the i got this erro at:

    tv = new TextView(this);

and error is: "The constructor TextView(new View.OnClickListener(){}) is undefined".

i knew i have to do:

    implements OnClickListener

but i cant change the prgrm now.SO is there anyway to create TextView(or any View objct) without refrence it to 'layout' xml?

    LinearLayout ll = (LinearLayout)findViewById(R.id.myLL);
    for(int i=1 ; i<= 10 ; i++){
      TextView tv = new TextView(this);
      tv.setText("String/String/String");
      ll.addView(tv);
      } 

THANKS,,


回答1:


You're adding the LinearLayout to itself. The last line should be ll.addView(tv);




回答2:


    LinearLayout ll = (LinearLayout)findViewById(R.id.myLL);
         for(int i=1 ; i<= 10 ; i++)
               {
                 TextView tv = new TextView(getApplicationContext());
                 tv.setText("String/String/String");
                 ll.addView(tv);
               }

replace 'new TextView(new)' with 'new TextView(getApplicationContext())' .



来源:https://stackoverflow.com/questions/23199133/android-draw-textedits-or-textviews-when-user-enter-number-of-them-exp-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!