我要做的东西是可以动态添加控件,并且这些控件的位置是可以通过拖动移动位置的。
这两点我都可以实现,但是问题是当我添加第二个控件的时候,第一个调整好的控件就会回到原来的位置。我尝试了很多方法都无法解决,希望这里的朋友可以给予帮助。代码内容我贴到下面请帮忙解决。
Activity代码:package bobo.viewtest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;public class Viewtest extends Activity {
    /** Called when the activity is first created. */
int btstat =0;
Button bt01=null;
LinearLayout cp =null;
LinearLayout vp =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt01=(Button)findViewById(R.id.bt01);
        cp=(LinearLayout)findViewById(R.id.cp);
        vp=(LinearLayout)findViewById(R.id.vp);
        bt01.setOnClickListener(new bt01listener());    }
    class bt01listener implements OnClickListener{
     EditText addname = new EditText(Viewtest.this);
  Button addnew = new Button(Viewtest.this);
  Button addnameback = new Button(Viewtest.this); @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (btstat==0){
addnew.setText("Add");
addnameback.setOnClickListener(new addnamebklistener());
addnew.setOnClickListener(new addnewnamelistener());
addnameback.setText("Back");
cp.addView(addname);
cp.addView(addnew);
cp.addView(addnameback);
btstat=1;
}
}
class addnamebklistener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
cp.removeView(addname);
cp.removeView(addnew);
cp.removeView(addnameback);
btstat=0;

}

}
     class addnewnamelistener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (addname.getText()!=null){
TextView nametext = new TextView (Viewtest.this);
nametext.setText(addname.getText());
nametext.setTextColor(0xffff00ff);
nametext.setClickable(true);
nametext.setOnTouchListener(new OnTouchListener(){
int[] temp = new int[] { 0, 0 };
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int eventaction = event.getAction();
                Log.i("&&&", "onTouchEvent:" + eventaction);
                int x = (int) event.getRawX() ;
                int y = (int) event.getRawY();
                switch (eventaction) {
                case MotionEvent.ACTION_DOWN: // touch down so check if the
                    temp[0] =(int) event.getX()+75 ; 
                    temp[1] = y - v.getTop();
                    break;                 case MotionEvent.ACTION_MOVE: // touch drag with the ball
                    v.layout(x - temp[0], y - temp[1], x + v.getWidth() - temp[0], y - temp[1] + v.getHeight()); 
                    v.postInvalidate(); //redraw
                    break;
                case MotionEvent.ACTION_UP:                 
                 //System.out.println(x - temp[0]);
                 //System.out.println(y - temp[1]);
                 //System.out.println( x + v.getWidth() - temp[0]);
                 //System.out.println(y - temp[1] + v.getHeight());
                
                
                    break;
                }
return false;

}

});
vp.addView(nametext);
}
cp.removeView(addname);
cp.removeView(addnew);
cp.removeView(addnameback);
btstat=0;
}    
     }
    }
}xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
<LinearLayout 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/cp"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ecard01"
android:layout_below="@id/cp"
android:orientation="vertical"
android:id="@+id/vp"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bp"
android:layout_below="@id/vp"
android:orientation="vertical"
>
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bt01"
    android:text="Add"
/>
</LinearLayout>
</LinearLayout>