Get Value of a Edit Text field

前端 未结 12 1088
既然无缘
既然无缘 2020-11-22 07:20

I am learning how to create UI elements. I have created a few EditText input fields. On the click of a Button I want to capture the content typed into that input field.

12条回答
  •  半阙折子戏
    2020-11-22 08:21

    You might also want to take a look at Butter Knife. It aims at reducing the amount of boilerplate code by using annotation. Here is a simple example:

    public class ExampleActivity extends ActionBarActivity {
    
        @InjectView(R.id.name)
        EditText nameEditText;
    
        @InjectView(R.id.email)
        EditText emailEditText;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_example);
            Butterknife.inject(this);
        }
    
        @OnClick(R.id.submit)
        public void onSubmit() {
             Editable name = nameEditText.getText();
             Editable email = emailEditText.getText();
        }
    }
    

    Just add the following dependency to your build.gradle:

    compile 'com.jakewharton:butterknife:x.y.z'
    

    As an alternative there is also AndroidAnnotations.

提交回复
热议问题