在滚动视图中,编辑文本有滚动功能,但在编辑文本中,如果我们键入的行数用户看不到编辑文本,则它会滚动到顶部。
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.
在我的布局中,我在可滚动编辑中有滚动视图 text.Both 正在使用 setOnTouchListener
工作。如果我们触摸 edittext,它是滚动的,布局滚动是交互的。
但是 问题 是当用户键入 edittext 时它移动到布局的顶部并且用户看不到编辑文本字段。
当时用户输入两个卷轴都有效。
我认为解决方案是当用户单击编辑文本时我们需要停止布局 scroll.In 这样编辑文本将对用户可见并且当用户 typing.But 我不知道如何实现时它不会移动如果有人知道请帮助我。
布局代码:-
<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>
主要Activity_code:-
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();
}
}
你可以检查这个代码。
提前致谢。
您可以使用线性布局作为父布局 class 而不是相对布局。这样编辑文本的位置应该根据其权重固定,并且在键入时可能会看到。
以下是我从其他 SO 问题中找到的答案,google.It 将在 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.
在我的布局中,我在可滚动编辑中有滚动视图 text.Both 正在使用 setOnTouchListener
工作。如果我们触摸 edittext,它是滚动的,布局滚动是交互的。
但是 问题 是当用户键入 edittext 时它移动到布局的顶部并且用户看不到编辑文本字段。
当时用户输入两个卷轴都有效。
我认为解决方案是当用户单击编辑文本时我们需要停止布局 scroll.In 这样编辑文本将对用户可见并且当用户 typing.But 我不知道如何实现时它不会移动如果有人知道请帮助我。
布局代码:-
<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>
主要Activity_code:-
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();
}
}
你可以检查这个代码。
提前致谢。
您可以使用线性布局作为父布局 class 而不是相对布局。这样编辑文本的位置应该根据其权重固定,并且在键入时可能会看到。
以下是我从其他 SO 问题中找到的答案,google.It 将在 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.