简体   繁体   English

从Android EditText字段返回用户输入

[英]Return user input from Android EditText field

I am new to Android dev. 我是Android开发人员的新手。 and to Programming in general. 和一般的编程。 I have a text based RPG I wrote in Java as a terminal app, and I am trying to recreate it as an Android app; 我有一个用Java作为终端应用程序编写的基于文本的RPG,并且我试图将其重新创建为Android应用程序。 however, I am getting stuck quite at the beginning. 但是,我一开始就陷入困境。

How can I return the user input from an EditText view and store that value in a String? 如何从EditText视图返回用户输入并将该值存储在String中?

Below is a throwaway project I made to figure out the problem: 以下是我为弄清问题而做的一次性项目:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String a = "Hello from";
    String b = "the other";
    String c = "SIDE!";
    String d = "What you don't like Adele?";

    final String userIn;

    final EditText editText = (EditText) findViewById(R.id.editText);
    TextView textView = (TextView) findViewById(R.id.viewText);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendMessage();
                handled = true;
            }
            return handled;
        }

        private void sendMessage() {
            userIn = editText.getText().toString();
        }
    });

}

I cannot change the value of userIn because it has to be final to work in the function. 我不能更改userIn的值,因为它必须是最终函数才能起作用。 I'm sure I'm missing something obvious, but any help would be grand! 我确定我缺少明显的东西,但是任何帮助都是伟大的!

Declare userin outside of onCreate() and will work to change it . 在onCreate()之外声明用户,并将对其进行更改。 Check this : 检查一下:

public class MainActivity extends AppCompatActivity {
String userIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String a = "Hello from";
    String b = "the other";
    String c = "SIDE!";
    String d = "What you don't like Adele?";



    final EditText editText = (EditText) findViewById(R.id.editText);
    TextView textView = (TextView) findViewById(R.id.viewText);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                sendMessage();
                handled = true;
            }
            return handled;
        }

        private void sendMessage() {
            userIn = editText.getText().toString();
        }
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM