Get Value of a Edit Text field

2018-12-31 12:52发布

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.

<EditText android:id="@+id/name" android:width="220px" />

That's my field. How can I get the content?

10条回答
余欢
2楼-- · 2018-12-31 13:28

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.

查看更多
梦醉为红颜
3楼-- · 2018-12-31 13:29
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  Button  rtn = (Button)findViewById(R.id.button);
  EditText edit_text   = (EditText)findViewById(R.id.edittext1);

    rtn .setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText value=", edit_text.getText().toString());
            }
        });
}
查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 13:36

Get value from an EditText control in android. EditText getText property use to get value an EditText:

EditText txtname = (EditText)findViewById(R.id.name);
String name      =  txtname.getText().toString();
查看更多
明月照影归
5楼-- · 2018-12-31 13:39

Try this code

final EditText editText = findViewById(R.id.name); // your edittext id in xml
Button submit = findViewById(R.id.submit_button); // your button id in xml
submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) 
    {
        String string = editText.getText().toString();
        Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
    }
});
查看更多
只若初见
6楼-- · 2018-12-31 13:40

By using getText():

Button   mButton;
EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.edittext);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText", mEdit.getText().toString());
            }
        });
}
查看更多
临风纵饮
7楼-- · 2018-12-31 13:41

I like shortcuts, so I do this.

getText(editText);
getText(button);
getText(textView);

Just make method in your BaseActivity / create BaseActivity if you don't have.

public class BaseActivity extends AppCompatActivity{
    public String getText(TextView tv) {
        return tv.getText().toString().trim();
    } 
}

And extend your all activities by this BaseActivity.

public class YourActivity extends BaseActivity {
  @Override
  public void onCreate(Bundle savedInstanceState){
     getText(editText);
     getText(button);
     getText(textView);
  }
}

Note that EditText, Button extends TextView, so I created only getText(TextView tv).

查看更多
登录 后发表回答