Android - create TextView (or EditText) programmat

2019-03-31 01:38发布

问题:

I am building an android app where the user paints some objects in the screen. One type of object is a Text object. The user creates the object by dragging his finger and the object is shown as a rectangle that can be moved/reshaped. When user taps the text object, I launch a new activity where user enters text which I return on the onActivityResult method.

Now I want to show the text in the object. I can have access to stuff as the coordinates of the Rectangle etc from my text class. What I want to do in essence, is to create a TextView (or EditText) programmatically and set its bounds as the bounds of my rectangle that my object is painted in. Is there a method that can help me do it?

(another approach would be to use the canvas.drawTextOnPath method in my text object. But this seems more complicated since my text might get out of the object, and I would also have to handle the multilines)

Thank you very much in advance!

EDIT: trying GAMA's approach

protected void onActivityResult(int requestCode, int resultCode, Intent data) {                 
  switch(requestCode) { 
  case 1:
      if (resultCode == Activity.RESULT_OK) { 
            String text=data.getStringExtra("text");
            System.out.println(text);
            TextView tv=new TextView(this);
            //LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams((int)texts.get(index).width,(int)texts.get(index).height));
            LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(100,100));
            //tv.setLayoutParams(lp);
            //lp.setMargins((int)texts.get(index).Sx, (int)texts.get(index).Sy, (int)texts.get(index).Lx, (int)texts.get(index).Ly);

            tv.setLayoutParams(lp);
            tv.setTextSize(10);
            tv.setTextColor(Color.RED);
            tv.setText(text);
            lp.setMargins(0,0,0,0);
            //tv.setVisibility(View.VISIBLE);
            System.out.println("got "+tv.getText());
            }
      break;
      }
  }  

both prints show the text as expected but I do not see anything in the screen (tried to set it on the bottom left to begin with)

回答1:

Actually, drawTextOnPath is your best bet. It won't bleed out. All you have to do is create a path that goes from the left, center vertical of your rectangle to the right center vertical. The method will take care of resizing the text so that it will stay within the Path.

You can adjust the path accordingly by using Paint.getTextWidth(). If the width is bigger than the box, extend your path with a line Paint.getTextHeight() below the first line.



回答2:

                    EditText edText = new EditText(this);
            edText .setId(i);
            edText .setLayoutParams(new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                    1f));

                    edText .setWidth(100);
            edText .setImeOptions(EditorInfo.IME_ACTION_NEXT);
            edText .setInputType(InputType.TYPE_CLASS_NUMBER);
            edText .setKeyListener(DigitsKeyListener.getInstance());
            edText .setMaxLines(1);
                    edText .setOnFocusChangeListener(this);
            edText .setOnEditorActionListener(this);
            edText .addTextChangedListener(this);

                    //this linearlayout id is declared inside your xml file
                        LinearLayout linear=(LinearLayout)findViewById(R.id.linearLayout1);
                        linear.addView(edText );


回答3:

Try this:

TextView tv=new TextView(this);
LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(width,height));
tv.setLayoutParams(lp);
lp.setMargins(0, 0, 0, 0);