麻烦的是添加多个编辑文本视图

时间:2014-02-07 05:34:42

标签: android scrollview android-edittext android-scrollview

我想在滚动视图中单击按钮时添加五个编辑文本视图。但是当我单击按钮时,出现的编辑文本视图不在滚动视图中,而是在主线性布局中  怎么办,编辑文本视图才能显示在滚动视图中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_editmessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_editMessage" />

    <EditText
        android:id="@+id/et_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:ems="10"
        android:inputType="textMultiLine"
        android:text="@string/et_messageToSend" >

        <requestFocus />
    </EditText>

    <ScrollView
        android:layout_width="match_parent"
        android:id="@+id/ScrView"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/et_display" >
        </EditText>
    </ScrollView>

    <Button
        android:id="@+id/bpickperson"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/b_pick" />

    <Button
        android:id="@+id/balert"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:text="@string/b_alert" />

    <Button
        android:id="@+id/baddmorecontacts"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:text="@string/b_addmorecontacts" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/b_facebook"
            android:layout_width="144dp"
            android:layout_height="wrap_content"
            android:text="@string/b_facebook" />

        <Button
            android:id="@+id/b_twitter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/b_twitter" />
    </LinearLayout>

</LinearLayout>

MainActivity

  public class MainActivity extends Activity implements LocationListener {
        String separator = "; ";
        private Button btn_cntct;
        public int REQUESTCODE = 1;
        private LinearLayout layoutLinear;
        private Button btn_addmore_cntct;
        int temp;
        ScrollView ScrView;
        Context c = MainActivity.this;
        Button b_alert;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    // defining button elements for picking contacts from phone-book
            btn_cntct = (Button) findViewById(R.id.bpickperson);
            btn_cntct.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    // using Intent for fetching contacts from phone-book
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                    startActivityForResult(intent, REQUESTCODE);
                }

            });

            // defining button and edit-text values for adding mutli edit-texts
            // views
                temp = 1;
            layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
            ScrView = (ScrollView)findViewById(R.id.ScrView);
            btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
            btn_addmore_cntct.setOnClickListener(OnClick());
            EditText editview = new EditText(this);
            editview.setText("Add more");

        }

        // implementing OnClickListener OnClick() method for "btn_addmore_cntct"
        // button
        private OnClickListener OnClick() {
            // TODO Auto-generated method stub
            // changing return type "null" to "new OnClickListner"
            return new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if (temp <= 2) {
                    EditText tab = new EditText(getApplicationContext());
                    tab.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                    tab.setEnabled(false);
                    tab.setFocusable(false);
//                  layoutLinear.addView(tab,0);
                    ScrView.addView(tab,0);
                    temp++;
                } else {
                    // Print message to user Cant add more editText.
                    Toast.makeText(getApplicationContext(), "No More Contacts",
                            Toast.LENGTH_SHORT).show();
                }


    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (data != null) {
                Uri uri = data.getData();
                Log.i("data", uri.toString());
                if (uri != null) {
                    Cursor c = null;
                    try {
                        c = getContentResolver()
                                .query(uri,
                                        new String[] {
                                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                                ContactsContract.CommonDataKinds.Phone.NUMBER,
                                                ContactsContract.CommonDataKinds.Phone.TYPE },
                                        null, null, null);

                        if (c != null && c.moveToFirst()) {
                            String name = c.getString(0);
                            String number = c.getString(1);
                            int type = c.getInt(2);

                            showSelectedNumber(name, number, type);
                        }
                    } finally {
                        if (c != null) {
                            c.close();
                        }
                    }
                }
            }
        }

        // defining showSelectedNummber to display selected contact from phone-book
        // to Edit-Text View
        String selectedNum = " ";

        public void showSelectedNumber(String name, String number, int type) {
            if (layoutLinear == null) {
                Log.i("layoutLinear is null", "null");
            } else {
                Log.i("layoutLinear is not  null", "not null");

            }

            EditText userNumber = (EditText) layoutLinear.getChildAt(0);
            if (userNumber == null) {
                Log.i("edittext is null", "null");
            } else {
                Log.i("edittext is not  null", "not null");

            }
            String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
                    .getTypeLabel(getResources(), type, "");
            // preventing number duplicacy and raising toast
            if (selectedNum.contains(number)) {
                // do nothing
                // alert user that number is already selected
                Toast.makeText(getApplicationContext(),
                        "Selected Contact Already Exists", Toast.LENGTH_SHORT)
                        .show();
            } else
                userNumber.setText(name + ":" + number + " " + typeNumber);
            selectedNum = selectedNum + number;

        }
    }

logcat的

02-07 11:44:46.116: D/AndroidRuntime(1281): Shutting down VM
02-07 11:44:46.116: W/dalvikvm(1281): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-07 11:44:46.126: E/AndroidRuntime(1281): FATAL EXCEPTION: main
02-07 11:44:46.126: E/AndroidRuntime(1281): java.lang.IllegalStateException: ScrollView can host only one direct child
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.widget.ScrollView.addView(ScrollView.java:220)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at com.example.zzz1.MainActivity$4.onClick(MainActivity.java:157)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.view.View.performClick(View.java:2485)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.view.View$PerformClick.run(View.java:9080)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.os.Handler.handleCallback(Handler.java:587)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.os.Looper.loop(Looper.java:123)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at java.lang.reflect.Method.invoke(Method.java:507)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-07 11:44:46.126: E/AndroidRuntime(1281):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

在scrollview ScrView.addView(tab)中添加edittext

答案 1 :(得分:0)

ScrollView必须有子布局才能添加任何视图。

将文本视图添加到线性布局 将LinearLayout添加到Scrollview。 将scrollview添加到父布局

这是xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/scLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

</LinearLayout>

这是活动java类

    LinearLayout l,scl;
    Button b1;
EditText e1,e2,e3,e4,e5;
ScrollView sv;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    l = (LinearLayout) findViewById(R.id.mainl);
    scl=(LinearLayout) findViewById(R.id.scLay);
    sv=(ScrollView)findViewById(R.id.scrollView1);
    b1=(Button)findViewById(R.id.button1);
    e1=new EditText(this);  
    e2=new EditText(this);  
    e3=new EditText(this);  
    e4=new EditText(this);  
    e5=new EditText(this);  
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            scl.addView(e1);
            scl.addView(e2);
            scl.addView(e3);
            scl.addView(e4);
            scl.addView(e5);
        }
    });
}