【问题标题】:Get text from edit text on popup Window从弹出窗口上的编辑文本中获取文本
【发布时间】:2014-08-11 14:42:38
【问题描述】:

我尝试使用输入到 EditText 中的文本,该 EditText 在 PopupWindow 上。这是我的 PopUpWindow 代码。

public void popupInit() {
            View popupView = null;
            popupView = inflater.inflate(R.layout.poplayout, null);

            popUp = new PopupWindow(popupView,             LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            popUp.setFocusable(true);
            popUp.setOutsideTouchable(isRestricted());
            popUp.setContentView(inflater.inflate(R.layout.poplayout, null, false));
            login = (Button) popupView.findViewById(R.id.loginButton);

        final EditText  username = (EditText) popupView.findViewById(R.id.username);
        final EditText  password = (EditText) popupView.findViewById(R.id.password);
            user_name =username.getText().toString();
            pass_word = password.getText().toString();


        }

运行时,上面的代码返回“”为用户名和密码,这是字段字符串。

谢谢。

【问题讨论】:

  • 我在提问之前尝试了那个链接,我又试了一次,仍然从 EditText 获得相同的空字符串。您能否指出上述代码中我可能遗漏的任何错误。
  • 就像一个一般问题... user_name 和 pass_word 被声明为字符串变量???
  • 您需要在关闭弹出窗口时执行 setContentView 和其余代码

标签: java android popupwindow modifiers


【解决方案1】:

这是你的工作代码,相应地改变:

简要说明:

单击 btnId 时会打开一个弹出窗口,当您输入用户名和密码并单击登录按钮时,弹出窗口将关闭,您输入的用户名密码将打印在日志中。检查您的 LogCat 以查看您输入的内容。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         final Button btn = (Button)findViewById(R.id.btnId);
         btn.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {

                 LayoutInflater layoutInflater 
                 = (LayoutInflater)getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE); 
                 View popupView = layoutInflater.inflate(R.layout.popup, null);  
               final View  popupView1 = layoutInflater.inflate(R.layout.popup, null);

                final PopupWindow popUp = new PopupWindow(popupView1,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                 popUp.setFocusable(true);
                 popUp.setOutsideTouchable(isRestricted());
                 Button login = (Button) popupView1.findViewById(R.id.loginButton);
                 login.setOnClickListener(new Button.OnClickListener(){

         @Override
         public void onClick(View v) {
             popUp.setContentView(popupView1);


         final EditText  username = (EditText) popupView1.findViewById(R.id.username);
         final EditText  password = (EditText) popupView1.findViewById(R.id.password);
             String user_name =username.getText().toString();
             String pass_word = password.getText().toString();
         Log.i("info",(user_name+pass_word));
          popUp.dismiss();
         }});

                 popUp.showAsDropDown(btn, 50, -30);
                 }


         });

    }

要回答您在 cmets 中发布的问题,您的 popupInit() 方法中应该有如下内容:

public void popupInit() {
         final LayoutInflater inflater 
         = (LayoutInflater)getBaseContext()
          .getSystemService(LAYOUT_INFLATER_SERVICE); 
       final View popupView = inflater.inflate(R.layout.popup, null);

       final PopupWindow popUp = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        popUp.setFocusable(true);
        popUp.setOutsideTouchable(isRestricted());

        Button login = (Button) popupView.findViewById(R.id.loginButton);
        login.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {

                 popUp.setContentView(inflater.inflate(R.layout.popup, null, false));
                 final EditText  username = (EditText) popupView.findViewById(R.id.username);
                    final EditText  password = (EditText) popupView.findViewById(R.id.password);
                        String user_name =username.getText().toString();
                        String pass_word = password.getText().toString();
                        Log.i("Username,password",(user_name+"-->"+pass_word));
                        popUp.dismiss();
            }

        });
         popUp.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }   

【讨论】:

    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多