我正在制作注册表单,其中我使用四个线性布局并使用View我将三个线性布局显示为下一个窗口。但我想骑过去 onBackPressed(){} 为此我该如何比较,我将如何扣除当前布局以及如何比较剩余的布局。这是我的xml
<LinearLayout 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:orientation="vertical" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/PD"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="FirstName*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="LastName*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Email*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Password*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="ConfirmPassword*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="DateofBirth*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Gender*" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="Next"
android:text="NEXT" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/PD1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="FirstName1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="LastName1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Email1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Password1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="ConfirmPassword1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="DateofBirth1*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Gender1*" />
<Button
android:id="@+id/next2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="Next2"
android:text="NEXT" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/PD1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="FirstName2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="LastName2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Email2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Password2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="ConfirmPassword2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="DateofBirth2*" />
<EditText
android:id="@+id/FN"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:hint="Gender2*" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="Next3"
android:text="NEXT" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
这是我的主要活动
public class MainActivity extends Activity {
View v;
LinearLayout l1,l2,l3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1 = (LinearLayout) findViewById(R.id.layout1);
l2 = (LinearLayout) findViewById(R.id.layout2);
l3 = (LinearLayout) findViewById(R.id.layout3);
}
public void Next(View v) {
v = (LinearLayout)findViewById(R.id.layout1);
v.setVisibility(View.GONE);
Log.i("IN 1st WIndow", "got it");
v = (LinearLayout)findViewById(R.id.layout2);
v.setVisibility(View.VISIBLE);
}
public void Next2(View v){
v = (LinearLayout) findViewById(R.id.layout2);
v.setVisibility(View.GONE);
Log.i("IN 2nd WIndow", "got it");
v = (LinearLayout) findViewById(R.id.layout3);
v.setVisibility(View.VISIBLE);
}
public void Next3(View v){
v = (LinearLayout) findViewById(R.id.layout2);
v.setVisibility(View.GONE);
Log.i("IN 3rd WIndow", "got it");
v = (LinearLayout) findViewById(R.id.layout3);
v.setVisibility(View.VISIBLE);
}
@Override
public void onBackPressed() {
if(l3.getVisibility == View.VISIBLE){
l3.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);
}else if(l2.getVisibility == View.VISIBLE){
l2.setVisibility(View.GONE);
l1.setVisibility(View.VISIBLE);
}else{
super.onBackPressed();
}
}
请帮我解决这个问题。抱歉我的英语很差。
答案 0 :(得分:1)
@Override
public void onBackPressed() {
if(LL3.getVisibility() == View.VISIBLE){
LL3.setVisibility(View.GONE);
LL2.setVisibility(View.VISIBLE);
}else if(LL2.getVisibility() == View.VISIBLE){
LL2.setVisibility(View.GONE);
LL1.setVisibility(View.VISIBLE);
}else{
super.onBackPressed();
}
}
在onCreate()
中 LL1,LL2,LL3 :
LL1 = (LinearLayout) findViewById(R.id.layout1);
LL2 = (LinearLayout) findViewById(R.id.layout2);
LL3 = (LinearLayout) findViewById(R.id.layout3);
答案 1 :(得分:0)
我相信这样的事情可行:
private int mPosition = 0;
private View[] mPages; // instantiate and populate this in onCreate
private void showNext() {
show(false, mPages[mPosition]);
show(true, mPages[++mPosition]);
}
private void showPrevious() {
show(false, mPages[mPosition]);
show(true, mPages[--mPosition]);
}
@Override
public void onBackPressed() {
showPrevious();
}
private static void show(final boolean show, final View view) {
final int visibility = show ? View.VISIBLE : View.GONE;
view.setVisibility(visibility);
}
当然,你必须确保mPosition
被正确绑定。
答案 2 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edtFirstName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="FirstName*" />
<EditText
android:id="@+id/edtLastName1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="LastName*" />
<EditText
android:id="@+id/edtEmail1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textEmailAddress"
android:hint="Email*" />
<EditText
android:id="@+id/edtPassword1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="Password*" />
<EditText
android:id="@+id/edtConfirmPassword1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="ConfirmPassword*" />
<EditText
android:id="@+id/edtDateOfBirth1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="date"
android:hint="DateofBirth*" />
<EditText
android:id="@+id/edtGender1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="Gender*" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<EditText
android:id="@+id/edtFirstName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="FirstName*" />
<EditText
android:id="@+id/edtLastName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="LastName*" />
<EditText
android:id="@+id/edtEmail2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textEmailAddress"
android:hint="Email*" />
<EditText
android:id="@+id/edtPassword2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="Password*" />
<EditText
android:id="@+id/edtConfirmPassword2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="ConfirmPassword*" />
<EditText
android:id="@+id/edtDateOfBirth2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="date"
android:hint="DateofBirth*" />
<EditText
android:id="@+id/edtGender2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="Gender*" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<EditText
android:id="@+id/edtFirstName3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="FirstName*" />
<EditText
android:id="@+id/edtLastName3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="LastName*" />
<EditText
android:id="@+id/edtEmail3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textEmailAddress"
android:hint="Email*" />
<EditText
android:id="@+id/edtPassword3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="Password*" />
<EditText
android:id="@+id/edtConfirmPassword3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="textPassword"
android:hint="ConfirmPassword*" />
<EditText
android:id="@+id/edtDateOfBirth3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="date"
android:hint="DateofBirth*" />
<EditText
android:id="@+id/edtGender3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="Gender*" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btnNextFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity{
private LinearLayout layout1;
private LinearLayout layout2;
private LinearLayout layout3;
private EditText edtFirstName1;
private EditText edtLastName1;
private EditText edtEmail1;
private EditText edtPassword1;
private EditText edtConfirmPassword1;
private EditText edtDateOfBirth1;
private EditText edtGender1;
private EditText edtFirstName2;
private EditText edtLastName2;
private EditText edtEmail2;
private EditText edtPassword2;
private EditText edtConfirmPassword2;
private EditText edtDateOfBirth2;
private EditText edtGender2;
private EditText edtFirstName3;
private EditText edtLastName3;
private EditText edtEmail3;
private EditText edtPassword3;
private EditText edtConfirmPassword3;
private EditText edtDateOfBirth3;
private EditText edtGender3;
private Button btnNextFinish;
private ArrayList<HashMap<String,String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = (LinearLayout)findViewById(R.id.layout1);
layout2 = (LinearLayout)findViewById(R.id.layout2);
layout3 = (LinearLayout)findViewById(R.id.layout3);
btnNextFinish = (Button)findViewById(R.id.btnNextFinish);
edtFirstName1 = (EditText)findViewById(R.id.edtFirstName1);
edtLastName1 = (EditText)findViewById(R.id.edtLastName1);
edtEmail1 = (EditText)findViewById(R.id.edtEmail1);
edtPassword1 = (EditText)findViewById(R.id.edtPassword1);
edtConfirmPassword1 = (EditText)findViewById(R.id.edtConfirmPassword1);
edtDateOfBirth1 = (EditText)findViewById(R.id.edtDateOfBirth1);
edtGender1 = (EditText)findViewById(R.id.edtGender1);
edtFirstName2 = (EditText)findViewById(R.id.edtFirstName2);
edtLastName2 = (EditText)findViewById(R.id.edtLastName2);
edtEmail2 = (EditText)findViewById(R.id.edtEmail2);
edtPassword2 = (EditText)findViewById(R.id.edtPassword2);
edtConfirmPassword2 = (EditText)findViewById(R.id.edtConfirmPassword2);
edtDateOfBirth2 = (EditText)findViewById(R.id.edtDateOfBirth2);
edtGender2 = (EditText)findViewById(R.id.edtGender2);
edtFirstName3 = (EditText)findViewById(R.id.edtFirstName3);
edtLastName3 = (EditText)findViewById(R.id.edtLastName3);
edtEmail3 = (EditText)findViewById(R.id.edtEmail3);
edtPassword3 = (EditText)findViewById(R.id.edtPassword3);
edtConfirmPassword3 = (EditText)findViewById(R.id.edtConfirmPassword3);
edtDateOfBirth3 = (EditText)findViewById(R.id.edtDateOfBirth3);
edtGender3 = (EditText)findViewById(R.id.edtGender3);
btnNextFinish = (Button)findViewById(R.id.btnNextFinish);
btnNextFinish.setText("Next");
btnNextFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isValidate=true;
if(layout1.getVisibility() == View.VISIBLE) {
if (edtFirstName1.getText().toString().trim().length() <= 0) {
edtFirstName1.setError("Value Required");
isValidate = false;
}
if (edtLastName1.getText().toString().trim().length() <= 0) {
edtLastName1.setError("Value Required");
isValidate = false;
}
if (edtPassword1.getText().toString().trim().length() <= 0) {
edtPassword1.setError("Value Required");
isValidate = false;
} else if (!edtConfirmPassword1.getText().toString().trim().equals(edtPassword1.getText().toString().trim())) {
edtConfirmPassword1.setError("Invalid Confirm Password");
isValidate = false;
}
if (edtDateOfBirth1.getText().toString().trim().length() <= 0) {
edtDateOfBirth1.setError("Value Required");
isValidate = false;
}
if (edtEmail1.getText().toString().trim().length() <= 0) {
edtEmail1.setError("Value Required");
isValidate = false;
} else if (!emailValidator(edtEmail1.getText().toString().trim())) {
edtPassword2.setError("Invalid Email");
isValidate = false;
}
if (edtGender1.getText().toString().trim().length() <= 0) {
edtGender1.setError("Value Required");
isValidate = false;
}
if (isValidate) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
layout3.setVisibility(View.GONE);
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("FirstName", edtFirstName1.getText().toString());
map1.put("LastName", edtLastName1.getText().toString());
map1.put("Email", edtEmail1.getText().toString());
map1.put("Password", edtPassword1.getText().toString());
map1.put("DateOfBirth", edtDateOfBirth1.getText().toString());
map1.put("Gender", edtGender1.getText().toString());
data.add(0, map1);
}
}else if(layout2.getVisibility() == View.VISIBLE) {
if (edtFirstName2.getText().toString().trim().length() <= 0) {
edtFirstName2.setError("Value Required");
isValidate = false;
}
if (edtLastName2.getText().toString().trim().length() <= 0) {
edtLastName2.setError("Value Required");
isValidate = false;
}
if (edtPassword2.getText().toString().trim().length() <= 0) {
edtPassword2.setError("Value Required");
isValidate = false;
} else if (!edtConfirmPassword2.getText().toString().trim().equals(edtPassword2.getText().toString().trim())) {
edtConfirmPassword2.setError("Invalid Confirm Password");
isValidate = false;
}
if (edtDateOfBirth2.getText().toString().trim().length() <= 0) {
edtDateOfBirth2.setError("Value Required");
isValidate = false;
}
if (edtEmail2.getText().toString().trim().length() <= 0) {
edtEmail2.setError("Value Required");
isValidate = false;
} else if (!emailValidator(edtEmail2.getText().toString().trim())) {
edtPassword2.setError("Invalid Email");
isValidate = false;
}
if (edtGender2.getText().toString().trim().length() <= 0) {
edtGender2.setError("Value Required");
isValidate = false;
}
if (isValidate) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.VISIBLE);
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("FirstName", edtFirstName2.getText().toString());
map2.put("LastName", edtLastName2.getText().toString());
map2.put("Email", edtEmail2.getText().toString());
map2.put("Password", edtPassword2.getText().toString());
map2.put("DateOfBirth", edtDateOfBirth2.getText().toString());
map2.put("Gender", edtGender2.getText().toString());
data.add(1, map2);
btnNextFinish.setText("Finish");
}
}else{
if (edtFirstName3.getText().toString().trim().length() <= 0) {
edtFirstName3.setError("Value Required");
isValidate = false;
}
if (edtLastName3.getText().toString().trim().length() <= 0) {
edtLastName3.setError("Value Required");
isValidate = false;
}
if (edtPassword3.getText().toString().trim().length() <= 0) {
edtPassword3.setError("Value Required");
isValidate = false;
} else if (!edtConfirmPassword3.getText().toString().trim().equals(edtPassword3.getText().toString().trim())) {
edtConfirmPassword3.setError("Invalid Confirm Password");
isValidate = false;
}
if (edtDateOfBirth3.getText().toString().trim().length() <= 0) {
edtDateOfBirth3.setError("Value Required");
isValidate = false;
}
if (edtEmail3.getText().toString().trim().length() <= 0) {
edtEmail3.setError("Value Required");
isValidate = false;
} else if (!emailValidator(edtEmail3.getText().toString().trim())) {
edtPassword3.setError("Invalid Email");
isValidate = false;
}
if (edtGender3.getText().toString().trim().length() <= 0) {
edtGender3.setError("Value Required");
isValidate = false;
}
if (isValidate) {
HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("FirstName", edtFirstName3.getText().toString());
map3.put("LastName", edtLastName3.getText().toString());
map3.put("Email", edtEmail3.getText().toString());
map3.put("Password", edtPassword3.getText().toString());
map3.put("DateOfBirth", edtDateOfBirth3.getText().toString());
map3.put("Gender", edtGender3.getText().toString());
data.add(2, map3);
}
}
}
});
data = new ArrayList<HashMap<String, String>>();
}
@Override
public void onBackPressed() {
if(layout3.getVisibility() == View.VISIBLE){
layout2.setVisibility(View.VISIBLE);
layout1.setVisibility(View.GONE);
layout3.setVisibility(View.GONE);
btnNextFinish.setText("Next");
}else if(layout2.getVisibility() == View.VISIBLE){
layout1.setVisibility(View.VISIBLE);
layout2.setVisibility(View.GONE);
layout3.setVisibility(View.GONE);
btnNextFinish.setText("Next");
}else{
super.onBackPressed();
}
}
public boolean emailValidator(final String mailAddress) {
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(mailAddress);
return matcher.matches();
}
}