Java 从弹出窗口上的编辑文本中获取文本
我试图使用输入到EditText中的文本,该EditText位于弹出窗口上。这是我的弹出窗口代码Java 从弹出窗口上的编辑文本中获取文本,java,android,popupwindow,modifiers,Java,Android,Popupwindow,Modifiers,我试图使用输入到EditText中的文本,该EditText位于弹出窗口上。这是我的弹出窗口代码 public void popupInit() { View popupView = null; popupView = inflater.inflate(R.layout.poplayout, null); popUp = new PopupWindow(popupView, LayoutParams
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\u name和pass\u word返回“”作为字段字符串
谢谢。这是您的工作代码,请相应更改: 简要说明: 单击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);
}
});
}
要回答在评论中发布的问题,您的popupInit()方法中应该有如下内容:
请参考此链接:我在提问之前尝试了该链接,但我只是再次尝试,仍然从EditText获得相同的空字符串。你能指出上面代码中我可能遗漏的任何错误吗。就像一般问题一样。。。user_name和pass_word声明为字符串变量???关闭弹出窗口时,需要执行setContentView和其余代码
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);
}