动态创建视图并添加到布局并使其可编辑

时间:2014-05-14 06:30:42

标签: java android textview

我创建了一个具有自定义文本视图的活动,可以在需要的地方将其拖放到屏幕上。我想动态添加此textview,并将从警告对话框中获取的文本添加到屏幕。下面是激活的代码。 作为菜单"添加文字"单击它将打开并alertDialog将从用户获取文本。现在当用户点击" OK"在提示对话框中,我想在屏幕上动态创建DragView。请指导我如何实现这一目标。

public class DragActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag);
        Intent intent = getIntent();
        int ImageId = intent.getIntExtra("drawableId", 0);
        this.findViewById(android.R.id.content).setBackgroundResource(ImageId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.drag, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.action_addText:
            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Add text");
            alert.setMessage("Please enter text to be displayed on image");

            // Set an EditText view to get user input 
            final EditText input = new EditText(this);
            alert.setView(input);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              String value = input.getText().toString();

              }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
              }
            });

            alert.show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }



}

自定义textview代码。

public class DragView extends TextView {

    private float mLastTouchX;
    private float mLastTouchY;

    private float mDeltaX;
    private float mDeltaY;

    public DragView(Context context) {
        super(context);
        init();
    }

    public DragView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setText("This is the text");
        setTextColor(Color.BLUE);
        setTextSize(20);

        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getAction();

                mLastTouchX = event.getRawX();
                mLastTouchY = event.getRawY();

                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
                    mDeltaX = mLastTouchX - lParams.leftMargin;
                    mDeltaY = mLastTouchY - lParams.topMargin;

                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    mLastTouchX = event.getRawX();
                    mLastTouchY = event.getRawY();

                    final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                    params.leftMargin = (int) (mLastTouchX - mDeltaX);
                    params.topMargin = (int) (mLastTouchY - mDeltaY);
                    setLayoutParams(params);

                    break;
                }
                }
                invalidate();

                return true;
            }
        });

        setOnLongClickListener( new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                setText("The text is changed");
                return false;
            }
        });
    }

}

XML文件 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DragActivity" >

    <com.example.dragview.DragView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

     <com.example.dragview.DragView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginTop="32dp"
         android:ems="10" >

         <requestFocus />
     </com.example.dragview.DragView>

</RelativeLayout>

enter image description here

2 个答案:

答案 0 :(得分:0)

我可以通过以下代码解决它。

alert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            String value = input.getText().toString();
                            final DragView textView = new DragView(
                                    getApplicationContext());
                            textView.setText(value);
                            textView.setTextColor(Color.BLUE);
                            final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                            textView.setLayoutParams(params);
                            relativeLayout.addView(textView, params);
                        }
                    });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // Canceled.
                        }
                    });

            alert.show();
            return true;

答案 1 :(得分:0)

我有简单的想法,使用Hashmap

来做到这一点

获取一个hashset并将所有视图放入key.like

 HashMap<String, View> lay = new HashMap<String, View>();

并将视图放在该hashmap

 lay.put("textview", title);

每当你想通过Hashmap密钥获取它

 View v = hashMap1.get("textview");