Java 我的对话框中EditText的TextWacther不工作或未启动
我正在尝试实现一个密码更新对话框,当用户单击对话框时,Java 我的对话框中EditText的TextWacther不工作或未启动,java,android,android-alertdialog,Java,Android,Android Alertdialog,我正在尝试实现一个密码更新对话框,当用户单击对话框时,EditText出现,AlertDialog弹出,EditText位于AlertDialog内,但不知怎的,TextWacther不起作用。我错过了什么 final android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(Landing_page.this); String title="Change Password";
EditText
出现,AlertDialog弹出,EditText
位于AlertDialog
内,但不知怎的,TextWacther
不起作用。我错过了什么
final android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(Landing_page.this);
String title="Change Password";
alert.setCancelable(false);
alert.setTitle(title); //Set Alert dialog title here
alert.setIcon(R.drawable.sett);
LinearLayout mainll = new LinearLayout(context);
LinearLayout alertLL = new LinearLayout(context);
alertLL.setOrientation(LinearLayout.HORIZONTAL);
alertLL.setPadding(0, 10, 0, 10);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
final TextView root = new TextView(context);
root.setPadding(10, 0, 10, 0);
root.setTextSize(14);
root.setTextColor(getResources().getColor(R.color.black));
root.setTypeface(tf);
root.setText("Old Password");
final EditText rootpat = new EditText(context);
rootpat.setLayoutParams(params);
rootpat.setTypeface(tff);
LinearLayout alertLL0 = new LinearLayout(context);
alertLL0.setPadding(0, 10, 0, 10);
alertLL0.setOrientation(LinearLayout.HORIZONTAL);
alertLL0.addView(root);
alertLL0.addView(rootpat);
// Set an EditText view to get user input
final TextView tv1 = new TextView(context);
tv1.setPadding(10, 0, 10, 0);
tv1.setTextColor(getResources().getColor(R.color.black));
tv1.setTextSize(14);
tv1.setTypeface(tf);
tv1.setText("New Password");
final EditText topic = new EditText(context);
topic.setLayoutParams(params);
topic.setTypeface(tff);
topic.setFilters(new InputFilter[] {new InputFilter.LengthFilter(16)});
topic.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length()<4&&s.toString().length()>16){
topic.setError("Password Should be between 8 and 16 Characters");
requestFocus(topic);
}
}
});
alertLL.addView(tv1);
alertLL.addView(topic);
final TextView tv4 = new TextView(context);
tv4.setPadding(10, 0, 10, 0);
tv4.setText("Confirm Password");
tv4.setTextColor(getResources().getColor(R.color.black));
tv4.setTextSize(14);
tv4.setTypeface(tf);
final EditText sensor = new EditText(context);
sensor.setLayoutParams(params);
sensor.setTypeface(tff);
sensor.setFilters(new InputFilter[] {new InputFilter.LengthFilter(16)});
sensor.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length()<4&&s.toString().length()>16){
sensor.setError("Password Should be between 8 and 16 Characters");
requestFocus(sensor);
}
}
});
LinearLayout alertLL4 = new LinearLayout(context);
alertLL4.setPadding(0, 10, 0, 10);
alertLL4.setOrientation(LinearLayout.HORIZONTAL);
alertLL4.addView(tv4);
alertLL4.addView(sensor);
mainll.setOrientation(LinearLayout.VERTICAL);
mainll.addView(alertLL0);
mainll.addView(alertLL);
mainll.addView(alertLL4);
alert.setView(mainll);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String pass1=rootpat.getEditableText().toString();
String pass2=topic.getEditableText().toString();
String pass3=sensor.getEditableText().toString();
if (pass2.equalsIgnoreCase(pass3)&&pass1.length()!=0&&pass2.length()!=0&&pass3.length()!=0){
final JSONObject object=new JSONObject();
try{object.put("user_name",user_name);
object.put("new_password",pass2);
object.put("password",pass1);
}catch (JSONException e){e.printStackTrace();}
try {
ClientResource clientResource = new ClientResource(IP.IP+"/api/users/upddate_password");
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
"admin", "admin");
Form form = new Form();
form.add("data", object.toString());
Representation representation = clientResource.post(form
.getWebRepresentation());
response=representation.getText().toString();
System.out.println(response);
try {
JSONObject object1=new JSONObject(response);
String mesg= (String) object1.get("message");
Toast.makeText(getApplicationContext(),mesg,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
//dialog.cancel();
}else {
Toast.makeText(getApplicationContext(),"Invalid Credentials.. Try again...",Toast.LENGTH_LONG).show();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
android.app.AlertDialog alertDialog = alert.create();
alertDialog.show();
alertDialog.setCancelable(false);
return true;
final android.app.AlertDialog.Builder alert=new android.app.AlertDialog.Builder(登录页面:this);
String title=“更改密码”;
警报。可设置可取消(错误);
警报。设置标题(标题)//在此处设置警报对话框标题
警报.setIcon(R.drawable.sett);
LinearLayout mainll=新的LinearLayout(上下文);
LinearLayout alertLL=新的LinearLayout(上下文);
alertLL.setOrientation(线性布局。水平);
alertLL.setPadding(0,10,0,10);
最终LinearLayout.LayoutParams参数=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容,1f);
最终文本视图根=新文本视图(上下文);
setPadding(10,0,10,0);
根的大小(14);
setextcolor(getResources().getColor(R.color.black));
root.setTypeface(tf);
root.setText(“旧密码”);
最终编辑文本rootpat=新编辑文本(上下文);
rootpat.setLayoutParams(参数);
rootpat.setTypeface(tff);
LinearLayout alertLL0=新的LinearLayout(上下文);
alertLL0.setPadding(0,10,0,10);
alertLL0.setOrientation(线性布局。水平);
alertLL0.addView(根目录);
alertLL0.addView(rootpat);
//设置EditText视图以获取用户输入
最终文本视图tv1=新文本视图(上下文);
tv1.设置填充(10,0,10,0);
tv1.setTextColor(getResources().getColor(R.color.black));
tv1.setTextSize(14);
tv1.setTypeface(tf);
tv1.setText(“新密码”);
最终编辑文本主题=新编辑文本(上下文);
topic.setLayoutParams(params);
topic.setTypeface(tff);
topic.setFilters(newInputFilter[]{newInputFilter.LengthFilter(16)});
topic.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
如果(s.toString().length()16){
topic.setError(“密码应该在8到16个字符之间”);
重点(专题);
}
}
});
alertLL.addView(tv1);
alertLL.addView(主题);
最终文本视图tv4=新文本视图(上下文);
tv4.setPadding(10,0,10,0);
tv4.setText(“确认密码”);
setextcolor(getResources().getColor(R.color.black));
tv4.setTextSize(14);
tv4.setTypeface(tf);
最终编辑文本传感器=新编辑文本(上下文);
传感器。setLayoutParams(参数);
传感器。设置字体(tff);
sensor.setFilters(新的InputFilter[]{new InputFilter.LengthFilter(16)});
sensor.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
如果(s.toString().length()16){
sensor.setError(“密码应该在8到16个字符之间”);
聚焦(传感器);
}
}
});
LinearLayout alertLL4=新的LinearLayout(上下文);
alertLL4.setPadding(0,10,0,10);
alertLL4.设置方向(线性布局。水平);
alertLL4.addView(tv4);
alertLL4.addView(传感器);
主要设置方向(线性布局、垂直);
mainl.addView(alertLL0);
mainl.addView(alertLL);
mainl.addView(alertLL4);
警报。设置视图(mainl);
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
字符串pass1=rootpat.getEditableText().toString();
字符串pass2=topic.getEditableText().toString();
String pass3=sensor.getEditableText().toString();
if(pass2.equalsIgnoreCase(pass3)和&pass1.length()!=0和&pass2.length()!=0和&pass3.length()!=0){
最终的JSONObject对象=新的JSONObject();
尝试{object.put(“user\u name”,user\u name);
object.put(“新密码”,pass2);
对象。put(“密码”,pass1);
}catch(JSONException e){e.printStackTrace();}
试一试{
ClientResource ClientResource=新的ClientResource(IP.IP+“/api/users/upddate_password”);
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
“管理员”、“管理员”);
表单=新表单();
add(“data”,object.toString());
Representation=clientResource.post(表单
.getWebRepresentation());
回应=
if (s.toString().length()<4&&s.toString().length()>16){
sensor.setError("Password Should be between 8 and 16 Characters");
requestFocus(sensor);
if (s.toString().length()<4 || s.toString().length()>16){
sensor.setError("Password Should be between 8 and 16 Characters");
requestFocus(sensor);
if (s.toString().length()<4&&s.toString().length()>16){
sensor.setError("Password Should be between 8 and 16 Characters");
requestFocus(sensor);
if ((s.toString().length()< 8) || (s.toString().length()>16)){
topic.setError("Password Should be between 8 and 16 Characters");
// requestFocus(topic);
}