如何从多行EditText获取字符串文本

时间:2019-05-06 10:45:28

标签: java android xml

我正在使用edittext来获取用户的输入。输入用户后,单击选项菜单按钮提交。提交后,如果用户输入为空,则应显示正常工作的“警告对话框”。但是即使用户在文本字段中输入了一些文本,它仍然显示为空输入。

  

XML

 <EditText
                android:id="@+id/post_summary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="top|left"
                android:hint="Share a photo, video or idea"
                android:inputType="textMultiLine"
                android:lines="10"
                android:maxLines="10"
                android:padding="10dp"
                android:minLines="2"
                android:background="#1AFFFFFF"
                android:textColor="@color/black"
                android:textSize="17sp"
                android:scrollbars="vertical"
                android:scrollbarStyle="outsideOverlay"
                android:textCursorDrawable="@color/black" />
  

Java

     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);
    }

1 个答案:

答案 0 :(得分:0)

您必须移动: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);
    }