【问题标题】:EditText doesnt take valueEditText 没有价值
【发布时间】:2018-06-04 09:48:12
【问题描述】:

我有两个 EditText,title 输入和 desc 输入。

    final EditText naslov_comm = view.findViewById(R.id.naslov_comm);
    final EditText opis_comm = view.findViewById(R.id.opis_comm);
    final String inputnaslov = naslov_comm.getText().toString();
    final String inputopis = opis_comm.getText().toString();
    objavicom_btn = view.findViewById(R.id.objavicom_btn);

    objavicom_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);

            sendNetworkRequest(id_post, user_pref, commentDTO_a);

        }
    });

每次单击我的按钮时,它都应该接受这些输入,但它返回空字符串

inputnaslov=""
inputopis=""

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hrle.android_portal.ReadPostActivity"
android:background="?android:windowBackground"
android:id="@+id/arpfc">



<EditText
    android:id="@+id/naslov_comm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Unesite naslov"
    android:inputType="text"

    />

<EditText
    android:id="@+id/opis_comm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/naslov_comm"
    android:hint="Unesite opis"
    android:inputType="text" />


<Button
    android:id="@+id/objavicom_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/opis_comm"
    android:layout_alignParentEnd="true"
    android:text="Objavi"
    />

最终会导致这个问题,或者可能是我的 xml 中的 android:inputType="text"?

【问题讨论】:

  • 你在用fragment吗?
  • 是的,我认为这就是问题所在

标签: java android input android-textinputedittext


【解决方案1】:

您应该将 getText().toString() 添加到 onClick(View view) 部分。

 objavicom_btn = view.findViewById(R.id.objavicom_btn);

    objavicom_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        final String inputnaslov = naslov_comm.getText().toString();
        final String inputopis = opis_comm.getText().toString();
        CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
        sendNetworkRequest(id_post, user_pref, commentDTO_a);


        }
    });

【讨论】:

  • @MarkoLazarevic 怎么样?请添加断点和调试。
【解决方案2】:

您正在读取 edittext 值的位置不正确。那时您的 editText 必须具有空值(因为这是您尝试读取动态值的声明块)。

所以当你想动态读取一个值时,最好的办法是点击按钮来读取它。


在点击按钮时移动值读取代码

inputnaslov = naslov_comm.getText().toString();
inputopis = opis_comm.getText().toString();

如下图

objavicom_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           // Read latest value of edittexts 
            inputnaslov = naslov_comm.getText().toString();
            inputopis = opis_comm.getText().toString();

            CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);

            sendNetworkRequest(id_post, user_pref, commentDTO_a);

        }
    });

【讨论】:

  • @MarkoLazarevic 现在什么不起作用?还是空文本?如果是,您对 editetxt 有什么要求吗?
【解决方案3】:

您所做的是,当 EditText 为空白时,您从 EditText 获取值,然后您将进一步使用该空白字符串。 正确的方法是当用户点击按钮时获取 EditText 的文本。

final EditText naslov_comm = view.findViewById(R.id.naslov_comm);
final EditText opis_comm = view.findViewById(R.id.opis_comm);
findViewById(R.id.objavicom_btn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        final String inputnaslov = naslov_comm.getText().toString();
        final String inputopis = opis_comm.getText().toString();
        CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
        sendNetworkRequest(id_post, user_pref, commentDTO_a);
    }
});

更新

最终会导致这个问题,或者可能是我的 xml 中的 android:inputType="text"?

不!它不会有问题,它只是input type 就像如果你设置它数字,用户将看到数字键盘并且只能输入数字到编辑文本。

【讨论】:

  • 这是你想要的东西的正确方法,如果它仍然不起作用,也许类更改没有在你的应用程序中折射。再次尝试卸载并安装新应用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2013-03-06
  • 1970-01-01
相关资源
最近更新 更多