EditText getText() returns empty string

时间秒杀一切 提交于 2019-12-05 03:44:25

You getText just after initialization. Untill you have text in xml you won't get the text. In onclick of alertdialog button get the text.

Declare

EdiText ed1,ed2; // before onCreate if in activity and onCraeteView in fragment

as a instance variable

View view = getLayoutInflater().inflate(R.layout.login_alert, null, false);
ed1= (EditText) view.findViewById(R.id.emailEditText))
ed2 = (EditText) view.findViewById(R.id.emailEditText);

then on Alert dialog Button click

  String email = ed1.getText().toString();
  String password= ed2.getText().toString()

you must get the text when you click on login button of alert dialog box

the above mentioned code you get text when you show alert dialog it always return always empty string you should follow the following procedure first you make a custom alert box layout having two edit text and one button user write text to edittext for login and give password and then click login button when you call login button click listener you can get text of edittext easyly

You are trying to get the text immediately after you inflated the view. Try doing it when the user clicks the done button instead.

Before onCreate add:

EditText email;
EditText pass;

Add this in your onCreate

etEmail (EditText) view.findViewById(R.id.emailEditText);
etPass (EditText) view.findViewById(R.id.emailEditText);

Then add this to when your button is clicked

String email = etEmail.getText().toString();
String pass = etEmail.getText().toString();

Alternatively add a TextChangedListener to you textview to change the change the string every time the textboxtext changes. A textwatcher is also possible

you should get the text when you click on save or done button. If you get this text on click of alert dialog button, you may end up taking it multiple times.

Just ensure that the editText.getText.toString() method is inside the OnClick() method, eg:

TextView submit = enquiryFragment.findViewById(R.id.query_submit_button);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){

            query_type = query_type_editText.getText().toString();
            query_text = query_editText.getText().toString();
            if (query_text.length()!=0 && query_type.length()!=0) {
                postQuery(query_type, query_text, store_id);
               // Log.e("query_type ",query_type );
            }else{
                Toast.makeText(getContext(), "Enter something !", Toast.LENGTH_SHORT).show();
            }
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!