Android:如何从InputField获取字符串值?

huangapple go评论82阅读模式
英文:

Android: How do I get the string value from inputfield?

问题

public class MainActivity extends AppCompatActivity {

    private TextToSpeech text_to_speech;
    private TextInputLayout one_voice;

    Dialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);

        myDialog = new Dialog(this);

        text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    text_to_speech.setLanguage(Locale.CANADA);
                }
            }
        });
    }

    public void speak(View v) {
        String to_speak = one_voice.getEditText().getText().toString();
        text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void OneShowPopup(View v) {
        TextView text_close;
        Button btnFollow = (Button) myDialog.findViewById(R.id.one_save_button);
        myDialog.setContentView(R.layout.command_one);
        text_close =(TextView) myDialog.findViewById(R.id.text_close);
        text_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();
    }
}
<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/command_one_voice"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:hint="@string/text">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.button.MaterialButton
    android:id="@+id/one_play_button"
    android:onClick="speak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="@string/play"
    style="?attr/borderlessButtonStyle" />
<com.google.android.material.button.MaterialButton
    android:onClick="OneShowPopup"
    android:id="@+id/command_one"
    android:layout_width="140dp"
    android:layout_height="100dp"
    android:padding="5dp"
    app:icon="@drawable/checked"
    android:textColor="#ffffff"
    android:text="Title 1"
    android:layout_marginRight="30dp"
    app:backgroundTint="#2BC9AF"
    app:iconGravity="textStart" />
英文:

I'm very new to Android, and I've been stuck on trying to get the value string from a material inputField component for hours.

To summarize, I have a button that clicks and opens up a dialog, and on the dialog there is an inputField and button. The button will read out what the inputField says using TextToSpeech.

I am confident that this is how you get the string from the material component (source: http://charmndroid.blogspot.com/2015/06/how-to-use-textinputlayout-in-android.html)

Everything works except for the string value extraction from inputField. If I only comment that out, everything works. Including the speech method (I tried with a static string and it works).

Could you please help me figure this out?

The error I'm getting:

2020-10-23 01:00:56.610 12012-12012/com.example.mint_anroidapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mint_anroidapp, PID: 31408
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5979)
at android.view.View.performClick(View.java:7125)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)
at android.view.View.performClick(View.java:7125)&#160;
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)&#160;
at android.view.View.performClickInternal(View.java:7102)&#160;
at android.view.View.access$3500(View.java:801)&#160;
at android.view.View$PerformClick.run(View.java:27336)&#160;
at android.os.Handler.handleCallback(Handler.java:883)&#160;
at android.os.Handler.dispatchMessage(Handler.java:100)&#160;
at android.os.Looper.loop(Looper.java:214)&#160;
at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)&#160;
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()&#39; on a null object reference
at com.example.mint_anroidapp.MainActivity.speak(MainActivity.java:49)
at java.lang.reflect.Method.invoke(Native Method)&#160;
at android.view.View$DeclaredOnClickListener.onClick(View.java:5974)&#160;
at android.view.View.performClick(View.java:7125)&#160;
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)&#160;
at android.view.View.performClickInternal(View.java:7102)&#160;
at android.view.View.access$3500(View.java:801)&#160;
at android.view.View$PerformClick.run(View.java:27336)&#160;
at android.os.Handler.handleCallback(Handler.java:883)&#160;
at android.os.Handler.dispatchMessage(Handler.java:100)&#160;
at android.os.Looper.loop(Looper.java:214)&#160;
at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
at java.lang.reflect.Method.invoke(Native Method)&#160;
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)&#160;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextToSpeech text_to_speech;
    private TextInputLayout one_voice;


    Dialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);

        myDialog = new Dialog(this);

        text_to_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    text_to_speech.setLanguage(Locale.CANADA);
                }
            }
        });
    }

    public void speak(View v) {
        String to_speak = one_voice.getEditText().getText().toString();
        text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void OneShowPopup(View v) {
        TextView text_close;
        Button btnFollow = (Button) myDialog.findViewById(R.id.one_save_button);
        myDialog.setContentView(R.layout.command_one);
        text_close =(TextView) myDialog.findViewById(R.id.text_close);
        text_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();
    }

command_one.xml

           &lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/command_one_voice&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:layout_width=&quot;250dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:hint=&quot;@string/text&quot;&gt;
&lt;com.google.android.material.textfield.TextInputEditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;com.google.android.material.button.MaterialButton
android:id=&quot;@+id/one_play_button&quot;
android:onClick=&quot;speak&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;8dp&quot;
android:layout_marginRight=&quot;8dp&quot;
android:text=&quot;@string/play&quot;
style=&quot;?attr/borderlessButtonStyle&quot; /&gt;

MainActivity.xml

&lt;com.google.android.material.button.MaterialButton
android:onClick=&quot;OneShowPopup&quot;
android:id=&quot;@+id/command_one&quot;
android:layout_width=&quot;140dp&quot;
android:layout_height=&quot;100dp&quot;
android:padding=&quot;5dp&quot;
app:icon=&quot;@drawable/checked&quot;
android:textColor=&quot;#ffffff&quot;
android:text=&quot;Title 1&quot;
android:layout_marginRight=&quot;30dp&quot;
app:backgroundTint=&quot;#2BC9AF&quot;
app:iconGravity=&quot;textStart&quot; /&gt;

答案1

得分: 1

one_voice =(TextInputLayout)findViewById(R.id.command_one_voice);

您试图在您的活动布局(MainActivity.xml)中查找视图,但它位于对话框布局(command_one.xml)中。您应该像这样做:

one_voice =(TextInputLayout)myDialog.findViewById(R.id.command_one_voice);

但是仅在您初始化此对话框之后。

英文:
one_voice = (TextInputLayout) findViewById(R.id.command_one_voice);

You try to find a view in your activity layout (MainActivity.xml), but it is in the dialog layout (command_one.xml). You should do something like this:

 one_voice = (TextInputLayout) myDialog.findViewById(R.id.command_one_voice);

But only after you init this dialog.

答案2

得分: 0

你需要在 <com.google.android.material.textfield.TextInputEditText 中添加 id,然后就可以直接从中获取文本。

在你的 XML 代码中添加 id,如下所示:

&lt;com.google.android.material.textfield.TextInputEditText
    android:id="@+id/edt_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /&gt;

然后在活动中添加以下行:

private TextInputEditText edtText;

onCreate 方法中添加以下行:

edtText = (TextInputEditText) findViewById(R.id.edt_text);

修改你的方法:

public void speak(View v) {
    String to_speak = edtText.getText().toString();
    text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}
英文:

You need to add id to <com.google.android.material.textfield.TextInputEditText then you can get text from it directly.

Add id to your xml code as below

 &lt;com.google.android.material.textfield.TextInputEditText
android:id=&quot;@+id/edt_text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;

Then add below lines to activity

 private TextInputEditText edtText;

In oncreate add below line

 edtText= (TextInputEditText) findViewById(R.id.edt_text);

Edit your method

 public void speak(View v) {
String to_speak = edtText.getText().toString();
text_to_speech.speak(to_speak, TextToSpeech.QUEUE_FLUSH, null);
}

huangapple
  • 本文由 发表于 2020年10月23日 16:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64496348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定