关于获取EditText编辑框中的值为空的问题

类似于这种注册页面的程序

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.regis_ui);
            EditText tv =  (EditText) findViewById(R.id.na);
            name = tv.getText().toString();
            EditText mm =  (EditText) findViewById(R.id.pd);
            password = mm.getText().toString();

            System.out.println(name+"-----"+password);
            bt_regis = (Button) findViewById(R.id.regis);
            bt_regis.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Toast.makeText(Regis.this, "注册成功!\n三秒后跳转至登页面",
                            Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Regis.this,Login.class);
                    intent.putExtra("name", name);
                    intent.putExtra("password", password);
                    Regis.this.setResult(0,intent);
                    finish();
                }
            });
}

此时是获取不到编辑框中的值,当把红色部分放置在按钮的监听函数中就可以获取到。

您好,感谢您的提问。关于您的问题,以下是一种可能的解决方案: 1. 在布局文件添加一个用于显示手机号码和验证码的弹窗。 在布局文件添加以下代码: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_get_verification_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取验证码" /> <!-- 弹窗布局 --> <RelativeLayout android:id="@+id/layout_popup" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:padding="16dp" android:visibility="gone"> <TextView android:id="@+id/tv_phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="手机号码:" /> <TextView android:id="@+id/tv_verification_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_phone_number" android:text="验证码:" /> <Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="关闭" /> </RelativeLayout> </LinearLayout> ``` 2. 在 Java 代码实现随机生成6位数号码,并在点击获取验证码按钮时显示弹窗 为按钮设置点击事件,生成6位数随机验证码并在弹窗显示手机号码和验证码,代码如下: ``` public class MainActivity extends AppCompatActivity { private EditText etPhoneNumber; private Button btnGetVerificationCode; private RelativeLayout layoutPopup; private TextView tvPhoneNumber; private TextView tvVerificationCode; private Button btnClose; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etPhoneNumber = findViewById(R.id.et_phone_number); btnGetVerificationCode = findViewById(R.id.btn_get_verification_code); layoutPopup = findViewById(R.id.layout_popup); tvPhoneNumber = findViewById(R.id.tv_phone_number); tvVerificationCode = findViewById(R.id.tv_verification_code); btnClose = findViewById(R.id.btn_close); // 为按钮设置点击事件 btnGetVerificationCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 获取手机号码 String phoneNumber = etPhoneNumber.getText().toString().trim(); // 生成6位数随机验证码 int verificationCode = new Random().nextInt(899999) + 100000; // 在弹窗显示手机号码和验证码 tvPhoneNumber.setText("手机号码:" + phoneNumber); tvVerificationCode.setText("验证码:" + verificationCode); layoutPopup.setVisibility(View.VISIBLE); } }); // 为关闭按钮设置点击事件 btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { layoutPopup.setVisibility(View.GONE); } }); } } ``` 这样就可以实现点击获取验证码按钮时,显示如下弹窗,其的手机号码为编辑框输入的号码,验证码通过随机生成6位数号码了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值