gpt4 book ai didi

java - 我无法从 EditText 获取值并使用该值

转载 作者:行者123 更新时间:2023-12-01 18:25:39 25 4
gpt4 key购买 nike

我几个小时都无法解决以下问题。我尝试从 EditText 获取用户输入的值,以便我可以使用它们。

public class Registrace extends AppCompatActivity implements View.OnClickListener {
private static final Pattern PASSWORD_PATTERN =
Pattern.compile("^" + "(?=.*[0-9])" +
"(?=.*[a-z])" + "(?=.*[A-Z])" + "(?=.*[@#$%^&+=])" + "(?=\\s+$)" + ".{6,}" + "$");


public EditText editPrijmeni, editJmeno, editEmail, editPwd, editPwd2;

public String Mess = null;

public String jmeno, prijmeni, email, heslo, heslo2;
Button btn;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_registrace);

TextView login = (TextView) findViewById(R.id.lnkLogin);
login.setMovementMethod(LinkMovementMethod.getInstance());
login.setOnClickListener(this);

editJmeno = (EditText) findViewById(R.id.txtJmeno);
jmeno = editJmeno.getText().toString().trim();

editPrijmeni = (EditText) findViewById(R.id.txtPrijmeni);
prijmeni = editPrijmeni.getEditableText().toString().trim();

editEmail = (EditText) findViewById(R.id.txtEmail);
email = editEmail.getEditableText().toString().trim();

editPwd = (EditText) findViewById(R.id.txtPassword);
heslo = editPwd.getEditableText().toString().trim();

editPwd2 = (EditText) findViewById(R.id.txtPassword2);
heslo2 = editPwd2.getEditableText().toString().trim();

btn = (Button) findViewById(R.id.btnRegistrace);
btn.setOnClickListener(this);

}

public boolean validujPrijmeni() {
if(prijmeni.isEmpty()) {
Mess = "Příjmení" + "\n";
return false;


} else {
return true;
}

}


@Override
public void onClick(View v) {


switch (v.getId()) {
case R.id.lnkLogin:
Intent intent = new Intent(Registrace.this, Login.class);
startActivity(intent);
break;
case R.id.btnRegistrace:
Toast.makeText(this, "Registrace", Toast.LENGTH_SHORT).show();

if(!prijmeni.isEmpty()) {
Toast.makeText(this, prijmeni, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Zadej příjmení", Toast.LENGTH_SHORT).show();
}
break;
}

}
}

为了确保 EditText 值正确存储,我尝试使用 Toast 文本显示它们。但是在填写值并按下按钮后,我仍然从 if 语句中获得“else” block ,以获取“switch”中的空 EditText。我不确定构造函数声明应该位于 onCreate 主体中还是外部。有人可以帮我吗???非常感谢。

这是layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

<TextView
android:id="@+id/loginscrn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Registrace"
android:textSize="20dp"
android:textStyle="bold" />

<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="15dp"
android:text="Příjmení" />
<EditText
android:id="@+id/txtPrijmeni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="15dp"
android:text="Jméno"/>
<EditText
android:id="@+id/txtJmeno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/thirdTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/fourTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heslo"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:inputType="textPassword"
android:ems="10" />

<TextView
android:id="@+id/fiveTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heslo, znova pro kontrolu"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/txtPassword2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:inputType="textPassword"
android:ems="10" />
<Button
android:id="@+id/btnRegistrace"
android:onClick="Registrace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Registrovat" />
<TextView android:id="@+id/lnkLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Jste již registrován?? Přihlaste se zde."
android:gravity="center"
android:textSize="20dp"
android:textColor="#3F51B5"
android:onClick="test"/>

最佳答案

执行此操作:在 onClick() 方法中执行以下操作:prijmeni = editPrijmeni.getEditableText().toString().trim();

您在创建 Activity 时执行此操作,因此 EditText 控件为空。它不会在更改时自动更新 prijmeni 字符串。

下面是经过一些小改进后的代码的样子。
它还可以进一步完善,但看起来你才刚刚开始,所以已经很好了。

public class Registrace extends AppCompatActivity implements View.OnClickListener {
private static final Pattern PASSWORD_PATTERN =
Pattern.compile("^" + "(?=.*[0-9])" +
"(?=.*[a-z])" + "(?=.*[A-Z])" + "(?=.*[@#$%^&+=])" + "(?=\\s+$)" + ".{6,}" + "$");

private EditText editPrijmeni;
private EditText editJmeno;
private EditText editEmail;
private EditText editPwd;
private EditText editPwd2;
private Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_registrace);

// Shouldn't you use a button instead of a text view?
// It seems you want a "Login" button to go to a LoginActivity
final TextView login = (TextView) findViewById(R.id.lnkLogin);
login.setMovementMethod(LinkMovementMethod.getInstance());
login.setOnClickListener(this);

// You could use https://jakewharton.github.io/butterknife which simplifies this boilerplate code a lot
editJmeno = (EditText) findViewById(R.id.txtJmeno);
editPrijmeni = (EditText) findViewById(R.id.txtPrijmeni);
editEmail = (EditText) findViewById(R.id.txtEmail);
editPwd = (EditText) findViewById(R.id.txtPassword);
editPwd2 = (EditText) findViewById(R.id.txtPassword2);
btn = (Button) findViewById(R.id.btnRegistrace);
btn.setOnClickListener(this);
}

public boolean validujPrijmeni() {
// if 'Mess' was for a validation message don't set it in the validation method.
// define a string in your resources.
// show/hide a TextView with that message from where you use the validation method.
final String prijmeni = editPrijmeni.getEditableText().toString().trim();
return !prijmeni.isEmpty();
}

// I would not declare the activity as a OnClickListener but use anonymous classes in the onCreate method
// calling method on this class.
// or if you use ButterKnife you can do so with OnClick annotations.
// This is to prevent the use of switch which is not great code.
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.lnkLogin:
Intent intent = new Intent(Registrace.this, Login.class);
startActivity(intent);
break;
case R.id.btnRegistrace:
Toast.makeText(this, "Registrace", Toast.LENGTH_SHORT).show();

final String prijmeni = editPrijmeni.getEditableText().toString().trim();
if (this.validujPrijmeni()) {
Toast.makeText(this, prijmeni, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Zadej příjmení", Toast.LENGTH_SHORT).show();
}
break;
}
}

}

关于java - 我无法从 EditText 获取值并使用该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60237360/

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