Java 如何从多行编辑文本中获取字符串文本
我正在使用edittext获取用户的输入。输入后,用户点击选项菜单按钮提交。提交后,如果用户输入为空,则应显示一个工作正常的警报对话框。但即使用户在文本字段中输入了一些文本,它仍然显示空输入 XMLJava 如何从多行编辑文本中获取字符串文本,java,android,xml,Java,Android,Xml,我正在使用edittext获取用户的输入。输入后,用户点击选项菜单按钮提交。提交后,如果用户输入为空,则应显示一个工作正常的警报对话框。但即使用户在文本字段中输入了一些文本,它仍然显示空输入 XML 您必须移动:st_text=data.getText().toString().trim(); 之后:int id=item.getItemId() 所以。。。您的代码应该如下所示: public boolean onOptionsItemSelected(MenuItem item) {
您必须移动:st_text=data.getText().toString().trim(); 之后:int id=item.getItemId() 所以。。。您的代码应该如下所示:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
st_text = data.getText().toString().trim();
if (id == R.id.post) {
if (TextUtils.isEmpty(st_text)){
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
String titleText = "Error \n ";
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark));
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(titleText);
ssBuilder.setSpan(
foregroundColorSpan,
0,
titleText.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
builder.setTitle(ssBuilder);
builder.setCancelable(false);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.setMessage("You can't create empty post");
dialog.show();
return false;
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
builder.setTitle("Success");
builder.setMessage("Your post is successfully created.");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_post);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("New Post");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
data = findViewById(R.id.post_summary);
st_text = data.getText().toString().trim();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.post) {
if (TextUtils.isEmpty(st_text)){
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
String titleText = "Error \n ";
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark));
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(titleText);
ssBuilder.setSpan(
foregroundColorSpan,
0,
titleText.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
builder.setTitle(ssBuilder);
builder.setCancelable(false);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.setMessage("You can't create empty post");
dialog.show();
return false;
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
builder.setTitle("Success");
builder.setMessage("Your post is successfully created.");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
st_text = data.getText().toString().trim();
if (id == R.id.post) {
if (TextUtils.isEmpty(st_text)){
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
String titleText = "Error \n ";
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark));
SpannableStringBuilder ssBuilder = new SpannableStringBuilder(titleText);
ssBuilder.setSpan(
foregroundColorSpan,
0,
titleText.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE
);
builder.setTitle(ssBuilder);
builder.setCancelable(false);
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.setMessage("You can't create empty post");
dialog.show();
return false;
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(CreatePostActivity.this,R.style.AlertDialog);
builder.setTitle("Success");
builder.setMessage("Your post is successfully created.");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}