从弹出窗口上编辑文本获取文本



我试图使用文本输入到EditText,其中EditText是在一个弹出窗口。这是我的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();

        }

运行时,上面的代码对于user_name和pass_word都返回",它们都是字段字符串。

谢谢。

这是为您准备的工作代码,请相应地更改:

简要说明:

点击btnId打开一个弹出窗口,输入用户名和密码,点击loginbutton,弹出窗口退出,输入用户名密码打印日志。检查你的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);
                 }

         });
    }

要回答您在评论中发布的问题,您应该在您的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);
    }   

相关内容

  • 没有找到相关文章

最新更新