简体   繁体   中英

Making android EditText vertical scrollable

I am new to Android development and I have an issue with making Android EditText vertically scroll able. The vertical scrolling of the EditText does not work for me. I followed several posts and resources but none of them worked for me. I am using the following code sample to enable the vertical scroll ability.

this.detailsEditText.setScroller(new Scroller(this));
this.detailsEditText.setMaxLines(1);
this.detailsEditText.setVerticalScrollBarEnabled(true);
this.detailsEditText.setMovementMethod(new ScrollingMovementMethod()); 

The following are the activity class and layout resource file, respectively I am using in my app.

CreateAppointmentActivity.java

package lk.iit.appointmentmanagerapp.activities;

import java.sql.Date;
import java.sql.Time;

import lk.iit.appointmentmanagerapp.data.Appointment;
import lk.iit.appointmentmanagerapp.data.DatabaseHandler;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Scroller;

public class CreateAppointmentActivity extends Activity {

    private EditText titleEditText; 
    private EditText timeEditText;
    private EditText detailsEditText;
    private EditText dateEditText;
    private Button saveButton;
    private Button resetButton;
    private final DatabaseHandler handler = new DatabaseHandler(this);

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

        this.titleEditText = (EditText)(this.findViewById(R.id.edit_title));
        this.timeEditText = (EditText)(this.findViewById(R.id.edit_time));
        this.detailsEditText = (EditText)(this.findViewById(R.id.edit_details));
        this.dateEditText = (EditText)(this.findViewById(R.id.edit_date));

        this.detailsEditText.setScroller(new Scroller(this));
        this.detailsEditText.setMaxLines(1);
        this.detailsEditText.setVerticalScrollBarEnabled(true);
        this.detailsEditText.setMovementMethod(new ScrollingMovementMethod());

        SharedPreferences preferences = getSharedPreferences("date_variables", MODE_PRIVATE);   
        this.dateEditText.setText(preferences.getInt("Year", 0) + "-" + preferences.getInt("Month", 0) + "-" + preferences.getInt("Day", 0));
        this.dateEditText.setEnabled(false);

        this.saveButton = (Button)(this.findViewById(R.id.save_new_appointment_button));
        this.saveButton.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                String[] dateComponents = dateEditText.getText().toString().split("-");
                String[] timeComponents = timeEditText.getText().toString().split(":");
                Appointment appointment = new Appointment();

                appointment.setAppointment_title(titleEditText.getText().toString());
                appointment.setAppointment_date(new Date(Integer.parseInt(dateComponents[0]), Integer.parseInt(dateComponents[1]), Integer.parseInt(dateComponents[2])));
                appointment.setAppointment_time(new Time(Integer.parseInt(timeComponents[0]), Integer.parseInt(timeComponents[1]), Integer.parseInt(timeComponents[2])));
                appointment.setAppointment_details(detailsEditText.getText().toString());

                boolean inserted = handler.addAppointment(appointment);
                //
                //
            }
        });
        this.resetButton = (Button)(this.findViewById(R.id.reset_button));
        this.resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                titleEditText.setText("");
                timeEditText.setText("");
                detailsEditText.setText("");
                dateEditText.setText("");
            }
        });
    }

}

activity_create_appointment.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/background"
    tools:context="lk.iit.appointmentmanagerapp.activities.CreateAppointmentActivity" >

    <TextView
       android:id="@+id/create_activity_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/welcome_create"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="15dip"
       android:layout_marginBottom="25dip"
       android:textSize="18.5sp"
       android:textColor="#ffffff" />

    <TextView
      android:id="@+id/title_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/create_activity_title"
      android:layout_marginTop="15dip"
      android:text="@string/title_textview_text"
      android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
       android:id="@+id/edit_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/create_activity_title"
       android:layout_marginTop="15dip"
       android:layout_toRightOf="@+id/title_textview"
       android:layout_marginLeft="30dip"
       android:background="#ffffff"
       android:ems="10"
       android:inputType="text" >
       <requestFocus />
   </EditText>

    <TextView
      android:id="@+id/time_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/title_textview"
      android:layout_marginTop="25dip"  
      android:text="@string/time_textview_text"
      android:textAppearance="?android:attr/textAppearanceMedium" 
      />

   <EditText
      android:id="@+id/edit_time"
      android:background="#ffffff"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/time_textview"
      android:layout_below="@+id/edit_title"
      android:layout_marginLeft="25dip"
      android:layout_marginTop="25dip"  
      android:ems="10"
      android:inputType="time" />

   <TextView
      android:id="@+id/details_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/time_textview"
      android:text="@string/details_textview_text"
      android:layout_marginTop="25dip"
      android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/edit_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/details_textview"
        android:layout_alignBottom="@+id/details_textview"
        android:layout_alignLeft="@+id/edit_title"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="text" >
    </EditText>


    <TextView
      android:id="@+id/date_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/details_textview"
      android:text="Date"
      android:layout_marginTop="25dip"
      android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/edit_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/date_textview"
        android:layout_below="@+id/edit_details"
        android:background="#ffffff"
        android:layout_marginLeft="25dip"
        android:layout_marginTop="25dip"    
        android:ems="10"
        android:inputType="date" >
    </EditText>


    <Button 
        android:id="@+id/save_new_appointment_button" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date_textview"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="10dip"
        android:layout_centerHorizontal="true"
        android:text="@string/save_button_text"
        android:textAllCaps="false" />
    <Button
        android:id="@+id/reset_button"  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/save_new_appointment_button"
        android:layout_marginTop="2dip"
        android:layout_centerHorizontal="true"
        android:text="@string/reset_button_text"
        android:textAllCaps="false" />

</RelativeLayout>

Please bear with me if I have done any mistakes as I am relatively new to this area of development. I would be grateful if someone help me out with this issue.

Try this in java code:

EditText dwEdit = (EditText) findViewById(R.id.DwEdit);       
dwEdit.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View view, MotionEvent event) {
                    // TODO Auto-generated method stub
                    if (view.getId() ==R.id.DwEdit) {
                        view.getParent().requestDisallowInterceptTouchEvent(true);
                        switch (event.getAction()&MotionEvent.ACTION_MASK){
                        case MotionEvent.ACTION_UP:
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                        }
                    }
                    return false;
                }
            });

And in your xml:

<EditText
        android:id="@+id/DwEdit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="10"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical" 
        android:overScrollMode="always"
        android:inputType="textCapSentences">
        </EditText> 

If you wanna scroll editText only in detailEditText then you specify height of editText 50dp or your need and remove inputType="text" from your editText ....it fulfill your need..

and you don't need any code in java relative to that editText..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM