gpt4 book ai didi

Android屏幕方向编辑文本显示设置错误?

转载 作者:行者123 更新时间:2023-12-05 07:53:38 25 4
gpt4 key购买 nike

我是 android 的新手,正在制作一个应用程序,其中有纵向和横向模式的布局设计。该应用程序在两个屏幕方向上都运行良好。在这个应用程序中,我在编辑文本上设置了验证并通过 set Error() 显示错误。 这工作正常但是当我尝试在编辑文本中使用空白字段旋转方向时 set Error() 方法设置错误.我试图通过谷歌搜索以不同的方式解决这个问题,但没有成功。请帮我。谢谢

下面是我的代码

EditText name, email, phone_no, subject, message;
Button send;
JSONObject json;
final Context context = this;
String valid_name = " ", valid_phone_no = " ", valid_email = " ",
valid_sub = " ", valid_msg = " ";
private ProgressDialog pDialog;
Boolean isInternetPresent = false;
Boolean isrotaion = false;
static boolean et_focus;
String emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
JsonParser jsonParser = new JsonParser();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_us);
name = (EditText) findViewById(R.id.edtname);
email = (EditText) findViewById(R.id.edtemail);
phone_no = (EditText) findViewById(R.id.edtphone);
subject = (EditText) findViewById(R.id.edtsubject);
message = (EditText) findViewById(R.id.edtmessage);
send = (Button) findViewById(R.id.send);

ActionBar actionBar = getActionBar();

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#0f567c")));
setTitle("Contact us");


send.setOnClickListener(new OnClickListener() {

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

if (valid_name != null && valid_email != null
&& valid_phone_no != null && valid_sub != null
&& valid_msg != null) {
isInternetPresent = isConnected();
if (!isInternetPresent) {

buildAlertMessageNonet();
} else

{
new CreateContact().execute();

}
} else {
Toast.makeText(getApplicationContext(),
"Please Fill up all Fields Correctly.",
Toast.LENGTH_SHORT).show();
}
}

});


int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
isrotaion = true;
} else {
// code for landscape mode
isrotaion = true;
}

name.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

validtion_name(name);

}
});
email.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
Log.i("TAG", "betextchange");
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

validation_Email(email);

}
});
phone_no.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

validation_Phone_no(10, 13, phone_no);

}
});
subject.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

validation_Subject(subject);

}
});

message.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

validation_Message(message);

}
});
}

public void validtion_name(EditText edt) throws NumberFormatException {
Log.i("TAG", "name");

Log.i("TAG", String.valueOf(edt.length()));
if (edt.getText().toString().trim().length() <= 0) {
edt.setError("Accept Alphabets Only.");
valid_name = null;
} else if (edt.getText().toString().contains(" ")) {
edt.setError(null);
} else if (!edt.getText().toString().matches("[a-zA-Z]+")) {
edt.setError("Accept Alphabets Only.");
valid_name = null;
} else if (edt.getText().toString().charAt(0) == ' ') {
edt.setError("First Letter Not be Space ");
valid_name = null;

} else {
valid_name = edt.getText().toString();
}

}

public void validation_Phone_no(int MinLen, int MaxLen, EditText edt)
throws NumberFormatException {

if (edt.getText().toString().length() <= 0) {
edt.setError("Numbers Only");
valid_phone_no = null;
} else if (edt.getText().toString().length() < MinLen) {
edt.setError("Minimum length " + MinLen);
valid_phone_no = null;

} else if (edt.getText().toString().length() > MaxLen) {
edt.setError("Maximum length " + MaxLen);
valid_phone_no = null;

} else if (edt.getText().toString().charAt(0) == ' ') {
edt.setError("First Number Not be Space");
valid_phone_no = null;
} else if (!edt.getText().toString().matches("^[+]?[0-9]{10,13}$")) {
edt.setError("Invalid Number");
valid_phone_no = null;

}

else {
valid_phone_no = edt.getText().toString();

}

}

public void validation_Email(EditText edt) {

if (edt.getText().toString() == null) {
edt.setError("Invalid Email Address");
valid_email = null;
} else if (isEmailValid(edt.getText().toString()) == false) {
edt.setError("Invalid Email Address");
valid_email = null;
} else if (edt.getText().toString().charAt(0) == ' ') {
edt.setError("First Letter Not be Space");
valid_email = null;
} else if (!edt.getText().toString().matches(emailPattern)) {
edt.setError("Invalid Email Address");
valid_email = null;
} else {
valid_email = edt.getText().toString();
}
}

boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

public void validation_Subject(EditText edt) {
if (edt.getText().toString().isEmpty()) {
edt.setError("Subject not be Empty ");
valid_sub = null;
} else if (edt.getText().toString().charAt(0) == ' ') {
edt.setError("First Letter Not be Space");
valid_sub = null;
} else {
valid_sub = edt.getText().toString();
}
}

public void validation_Message(EditText edt) {
Log.i("TAG", "maessage");
if (edt.getText().toString().isEmpty()) {
edt.setError("Message not be Empty ");
valid_msg = null;

} else if (edt.getText().toString().charAt(0) == ' ') {
edt.setError("First Letter Not be Space");
valid_msg = null;
} else {
edt.setError(null);
valid_msg = edt.getText().toString();
}
}

也在 list 中定义 orination

 <application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.my.Home"
android:configChanges="orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.my.Careers"
android:configChanges="orientation"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.my.Services"
android:configChanges="orientation" >
</activity>
<activity
android:name="com.my.Contact_us"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.my.View_service"
android:configChanges="orientation" >
</activity>
<activity
android:name="com.my.Direction"
android:configChanges="orientation" >
</activity>

最佳答案

每当您更改方向时,所有 View 都会重新生成。再次调用 OnCreateView 方法,再次将 TextChangedListener 添加到调用其方法的 editText View,最终在空白 editText 上调用 setError。要检查屏幕方向是否已更改并且在 EditText 中未输入任何内容,我们可以使用在 Activity 的 OnCreate() 方法中传递的 Bundle,如果 Activity 是新创建的,则此 bundle 为 null 但当屏幕方向更改时它不为 null .查看下面的代码,我只为名称 EditText 实现了它,但我想你必须对所有 EditText View 执行相同的操作,因为它取决于方向更改时光标所在的 View 。将包声明为类成员,

    Bundle mSavedInstanceState;

并将传递的包保存在 OnCreate() 方法中,

        if(savedInstanceState != null){
this.mSavedInstanceState = savedInstanceState;
}

现在在检查错误时使用这个包,如果它的方向改变(即包不会为空)不显示错误...用下面的代码替换你的代码,P.S.有些条件不对,我已经修复了,希望能帮助到你..!让我知道它是否适合您,并将其标记为答案,以便对其他人有用。

    public void validtion_name(EditText edt) throws NumberFormatException {
Log.i("TAG", "name");

Log.i("TAG", String.valueOf(edt.length()));
if (edt.getText().toString().trim().length() <= 0 && mSavedInstanceState ==null) {
edt.setError("Accept Alphabets Only.");
valid_name = null;
} else if (edt.getText().toString().contains(" ")) {
edt.setError(null);
} else if (edt.getText().toString().trim().length() > 0 && !edt.getText().toString().matches("[a-zA-Z]+")) {
edt.setError("Accept Alphabets Only.");
valid_name = null;
} else if (edt.getText().toString().length() > 0) {
if(edt.getText().toString().charAt(0) == ' '){
edt.setError("First Letter Not be Space ");
valid_name = null;
}
} else {
valid_name = edt.getText().toString();
}
}

关于Android屏幕方向编辑文本显示设置错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com