简体   繁体   中英

How to getText from another Layout from editText Android

I am making an activity in which an alertDialog will appear. The dialog will have a view which contains: Linear layout with two editText (Numeric texts) and then setHeight(wrapContent). You can see below.

在此处输入图像描述

Hope you are understanding now what i am doing. Here the view is a xml file which i created and it is not the activity's xml file. Now I want that when a user put the pin in the editText then i can get it and confirm it or whatever i want. But the problem is that i am not getting anything when i call getText(). Here is my code.


LayoutInflater factory = getLayoutInflater();
        View view = factory.inflate(R.layout.zalert_pass_enable, null); // zalert_pass.. is the xml file

        EditText passInput1 = view.findViewById(R.id.inputPass);
        EditText passInput2 = view.findViewById(R.id.inputPassConfirm);

       String value = passInput1.getText().toString().trim();

I edited my code to so that you can understand, this is not the actual code but it can easily tell my problem. When i toast the value String then i get nothing. Like there are 0 charaters. But i am putting 4 charaters at least each time. Now i know that the problem is in the process of linking the xml file to inflator.

 infalInflater.inflate(R.layout.list_item, parent, false);

See the above line of code. This a well upvoted answer of stackOverflow . which tells us that we should not null the inflator. We should give it parent. But what will be the parent in my case.

在此处输入图像描述

You can see the picture and that is my problem and i want a solution for that, please help.

Add this line Dialog dialog = new Dialog(context); after you are inflating view and use it to get edittext object like this

LayoutInflater factory = getLayoutInflater();
    View view = factory.inflate(R.layout.zalert_pass_enable, null); 
    Dialog dialog = new Dialog(context); // add this line
    EditText passInput1 = dialog.findViewById(R.id.inputPass);
    EditText passInput2 = dialog.findViewById(R.id.inputPassConfirm);

   String value = passInput1.getText().toString().trim();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM