【问题标题】:In Scroll View Edit text has scroll both are worked but in edit text if we type no of lines user can't see edit text it is scroll to top.在滚动视图中,编辑文本都可以滚动,但在编辑文本中,如果我们输入 no of lines,用户看不到编辑文本,它会滚动到顶部。
【发布时间】:2016-08-24 06:38:23
【问题描述】:

在我的布局中,我在该可滚动编辑文本中有滚动视图。两者都使用setOnTouchListener 工作。如果我们触摸edittext,它就是滚动,布局滚动是交互的。 但是问题是当用户输入edittext时,它会移动到布局的顶部,并且用户看不到编辑文本字段。 当时用户输入两个滚动条都有效。 我认为解决方案是当用户单击编辑文本时,我们需要停止布局滚动。这样编辑文本将对用户可见,并且在用户键入时它不会移动。但如果有人知道,我不知道如何实现它请帮帮我。

布局代码:-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Welcome To Register Page"
    android:textSize="20dp"
    android:textStyle="bold"
    android:id="@+id/title"
    android:textColor="@android:color/holo_blue_bright"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginTop="5dp"
        android:orientation="vertical"
        android:id="@+id/lin1"
        android:background="@drawable/shape">
     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Personal Details"
         android:gravity="center"/>
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dp"
          android:orientation="horizontal">
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="First Name :"/>
          <EditText
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/fname"
              android:inputType="textCapSentences"
              android:imeOptions="actionNext"
              android:layout_marginLeft="5dp"/>
      </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Last Name :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lname"
                android:inputType="textCapSentences"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phone No :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/pno"
                android:inputType="number"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/lin1"
        android:id="@+id/lin2"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Address"
            android:gravity="center"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="City/Town :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/city"
                android:inputType="textCapSentences"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="State :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/state"
                android:inputType="textCapSentences"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Country :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/country"
                android:inputType="textCapSentences"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Zip Code :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/zipcode"
                android:inputType="number"
                android:imeOptions="actionNext"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lin3"
        android:layout_below="@id/lin2"
        android:orientation="vertical"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Enter Full Address"
            android:gravity="center"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="10"
            android:lines="10"
            android:maxEms="1000"
            android:scrollbars="vertical"
            android:layout_marginTop="5dp"
            android:id="@+id/address"
            android:gravity="top"
            android:background="@drawable/shape"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lin4"
        android:layout_below="@id/lin3"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Other Details"
            android:gravity="center"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="E-Mail :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date of Birth :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dob"
                android:focusable="false"
                android:imeOptions="actionNone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lin5"
        android:layout_below="@id/lin4"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Other Details"
            android:gravity="center"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="E-Mail :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email_dum"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date of Birth :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dob_dum"
                android:focusable="false"
                android:imeOptions="actionNone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lin6"
        android:layout_below="@id/lin5"
        android:orientation="vertical"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Other Details"
            android:gravity="center"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="E-Mail :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/email_dum1"
                android:singleLine="true"
                android:imeOptions="actionDone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date of Birth :"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/dob_dum1"
                android:focusable="false"
                android:imeOptions="actionNone"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/lin6"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:id="@+id/submit"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/clear"
            android:text="Clear"
            android:layout_marginLeft="5dp"/>

    </LinearLayout>
</RelativeLayout>
</ScrollView>

主要活动代码:-

public class MainActivity extends AppCompatActivity {

    EditText first_name,last_name,phone_no,city,state,country,zip_code,full_address,email,dob;

    Button submit,clear;
    private int year;
    private int month;
    private int day;
    private Calendar cal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        first_name=(EditText)findViewById(R.id.fname);
        last_name=(EditText)findViewById(R.id.lname);
        phone_no=(EditText)findViewById(R.id.pno);
        city=(EditText)findViewById(R.id.city);
        state=(EditText)findViewById(R.id.state);
        zip_code=(EditText)findViewById(R.id.zipcode);
        country=(EditText)findViewById(R.id.country);
        full_address=(EditText)findViewById(R.id.address);
        email=(EditText)findViewById(R.id.email);
        dob=(EditText)findViewById(R.id.dob);

        submit=(Button) findViewById(R.id.submit);
        clear=(Button) findViewById(R.id.clear);

        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(MainActivity.this,result.class);

                Bundle bundle=new Bundle();
                bundle.putString("First_Name",first_name.getText().toString());
                bundle.putString("Last_Name",last_name.getText().toString());
                bundle.putString("Phone_No",phone_no.getText().toString());
                bundle.putString("City",city.getText().toString());
                bundle.putString("State",state.getText().toString());
                bundle.putString("Country",country.getText().toString());
                bundle.putString("Zip_Code",zip_code.getText().toString());
                bundle.putString("Full_Address",full_address.getText().toString());
                bundle.putString("Email",email.getText().toString());
                bundle.putString("DOb",dob.getText().toString() );

                intent.putExtras(bundle);

                startActivity(intent);
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                first_name.setText("");
                last_name.setText("");
                phone_no.setText("");
                city.setText("");
                state.setText("");
                country.setText("");
                zip_code.setText("");
                full_address.setText("");
                email.setText("");
                dob.setText("");

            }
        });

        dob.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateDialog();
            }
        });
        full_address.setOnTouchListener(new EditText.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                // Handle ListView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });
    }

    private void DateDialog() {
        DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {

                dob.setText(dayOfMonth+"/"+monthOfYear+"/"+year);

            }};


        DatePickerDialog dpDialog=new DatePickerDialog(this, listener, year, month, day);
        dpDialog.show();
    }
}

你可以检查这个代码。

提前致谢。

【问题讨论】:

    标签: android android-edittext scrollview


    【解决方案1】:

    您可以使用线性布局作为父类而不是相对布局。这样编辑文本的位置应该通过它的权重来固定,并且在输入时它可能是可见的。

    【讨论】:

    • 问题不在布局父级是滚动视图。如果输入更多行,如 15 编辑文本对用户不可见。因为它被滚动到布局的顶部。
    【解决方案2】:

    以下是我从其他 SO 问题和 google 中找到的答案。当 edittext 聚焦父滚动不起作用时,它会有所帮助。

    full_address.setOnTouchListener(new EditText.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                // Disallow ScrollView to intercept touch events.
                                v.getParent().requestDisallowInterceptTouchEvent(true);
                                break;
    
                            case MotionEvent.ACTION_UP:
                                // Allow ScrollView to intercept touch events.
                                v.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                        }
    
                        // Handle ListView touch events.
                        v.onTouchEvent(event);
                        return true;
                    }
                });
    

    答案仍然不完整。

    以下链接将对您有所帮助。

    Stop Scroll view

    Stop scrollview from seting focus.

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2016-02-04
      • 1970-01-01
      相关资源
      最近更新 更多