从editText.getText().toString()赋值后,字符串变量为空



我从导航抽屉中创建了一个弹出菜单,一切都与实际弹出菜单有关。但是,我在弹出窗口中有一个EditText,希望用户在其中输入文本,然后单击按钮做其他事情。我的问题是,当我将EditText.getText((.toString((值分配给函数中的字符串变量时,字符串变量仍然是空的,当我检查EditText.getText((.toString(是否为空时,它不是空的。所以我不明白对字符串变量的赋值是如何在变量中返回空字符串的。我真的是Android编程的新手,所以任何建议都将不胜感激。非常感谢。

onCreate方法的代码段

public class workouts extends AppCompatActivity {
private ActionBarDrawerToggle mToggle;
private static final String TAG = "Workouts";
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listHeader;
private HashMap<String, List<String>> listHashMap;

// Pop up menu variables
private EditText categoryTitle;
private Dialog myDialog;
private String headerTitle = "";
private boolean validTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workouts);
DrawerLayout mDrawerLayout = findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final NavigationView navigationView = (NavigationView)findViewById(R.id.navigationView);
listView = (ExpandableListView)findViewById(R.id.expandable_list_view);
initializeData();
listAdapter = new expandableListAdapter(this, listHeader, listHashMap);
listView.setAdapter(listAdapter);
// Pop-up menu
myDialog = new Dialog(this);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.back:
{
finish();
break;
}
case R.id.addCategory:
{
//View header = getLayoutInflater().inflate(R.layout.list_group, null);
//listView.addHeaderView(header);
/*
1) Create pop-up menu w/ EditText box
2) Type in header group to add
3) Click create button
4) Add new header to list
*/
try {
ShowPopup(item);
}
catch (Exception e)
{
Log.d(TAG, "Add pop-up: " + e.toString());
}
break;
}

创建弹出窗口的功能我试图修复的字符串是headerTitle

public void ShowPopup(MenuItem menuItem)
{
TextView textClose;
Button btnAddCategory;
myDialog.setContentView(R.layout.add_category_popup);
textClose = (TextView)myDialog.findViewById(R.id.textClose);
btnAddCategory = (Button)myDialog.findViewById(R.id.addCategory);
categoryTitle = (EditText)myDialog.findViewById(R.id.category_title);
headerTitle = categoryTitle.getText().toString();    // Header title to be used as hash key
Log.i(TAG, "ShowPopup: Assigned header title");
Log.i(TAG, "ShowPopup: " + headerTitle);
textClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
btnAddCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(categoryTitle.getText().toString().isEmpty())
{
categoryTitle.setError("Field cannot be empty");
categoryTitle.requestFocus();
validTitle = false;
}
else
{
categoryTitle.setError(null);
validTitle = true;
}
if(validTitle) {
listHeader.add(headerTitle);    // Add header title to list of headers
List<String> newHeader = new ArrayList<>(); // Create new expandable list w/ 0 items
listHashMap.put(listHeader.get(listHeader.size() - 1), newHeader);
myDialog.dismiss();
}
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}

您应该首先将值分配给变量:

btnAddCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
headerTitle = categoryTitle.getText().toString()
if(headerTitle.isEmpty())
{
categoryTitle.setError("Field cannot be empty");
categoryTitle.requestFocus();
validTitle = false;
}
else
{
categoryTitle.setError(null);
validTitle = true;
}
if(validTitle) {
listHeader.add(headerTitle);    // Add header title to list of headers
List<String> newHeader = new ArrayList<>(); // Create new expandable list w/ 0 items
listHashMap.put(listHeader.get(listHeader.size() - 1), newHeader);
myDialog.dismiss();
}
}
});

相关内容

  • 没有找到相关文章

最新更新