ChatGPT解决这个技术问题 Extra ChatGPT

Get Value of a Edit Text field

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?

The amout of upvotes somewhat proves that overriding toString does not yield the most discoverable API, however fancy is the technique.
The grammar and sentiment in your comment is really hard to understand...
@vemv The problem with the API is returning an Editable object where users expect and need a simple String 99% of times.
I think Android is cool, but I am surprised I had to Google for this (and for how to detect when the value has changed and is ready to read out, which can be complicated). I think they have made this harder than necessary!

F
FerranB

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());
            }
        });
}

If you don't place EditText in the clas-wide scope, and instead declare it in OnCreate, it wants it to be declared as final. I assume it is better not to make it final, but is the only way to do so, to make it class-wide? IS making it class-wide the best way to go about this?
@PFranchise What do you mean by "class-wide"? Package protected (as in the example)? Private? Why wouldn't you want to have a widget accessible in the whole class?
Not a request for more information or suggestion. Also not a thanks or +1. THIS IS TERRIBLE! Why would anyone think this is the right way to design a text box. Also, EditText is the least intuitive name they could find.
Z
Zoltan Toth

I guess you will have to use this code when calling the "mEdit" your EditText object :

myActivity.this.mEdit.getText().toString()

Just make sure that the compiler know which EditText to call and use.


you should add explanation where does the "mEdit" come from.
M
Mike Yang

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

EditText txtname = findViewById(R.id.name);
String name      =  txtname.getText().toString();

A
Andy

I hope this one should work:

Integer.valueOf(mEdit.getText().toString());

I tried Integer.getInteger() method instead of valueOf() - it didn't work.


This is assuming the value is an Integer... not sure where you picked that up in the question.
A
Afzaal Ahmad Zeeshan
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());
            }
        });
}

J
JJD

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.


C
Community

Shortest & Simplest

getText(editText);

getText(button);

getText(textView);

Little Workaround

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).


M
Malik Ali

String value = YourEditText.getText().toString;


M
MB-F

A more advanced way would be to use butterknife bindview. This eliminates redundant code.

In your gradle under dependencies; add this 2 lines.

compile('com.jakewharton:butterknife:8.5.1') {
        exclude module: 'support-compat'
    }
apt 'com.jakewharton:butterknife-compiler:8.5.1'

Then sync up. Example binding edittext in MainActivity

import butterknife.BindView;   
import butterknife.ButterKnife; 

public class MainActivity {

@BindView(R.id.name) EditTextView mName; 
...

   public void onCreate(Bundle savedInstanceState){
         ButterKnife.bind(this); 
         ...
   }

}

But this is an alternative once you feel more comfortable or starting to work with lots of data.


M
Martijn Pieters

step 1 : create layout with name activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#c6cabd"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textColor="#ff0e13"
        />
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:hint="Input your country"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get EditText Text"
        android:layout_below="@id/et"
        />
</RelativeLayout>

Step 2 : Create class Main.class

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.btn);
        final TextView tv = (TextView) findViewById(R.id.tv);
        final EditText et = (EditText) findViewById(R.id.et);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String country = et.getText().toString();
                tv.setText("Your inputted country is : " + country);
            }
        });
 }
}

K
Kunu

If you are using Kotlin and wants to make it generic you can use the Extension function

Extension function:

fun EditText.getTextString(): String {
    this.text.toString()
}

and call this method directly from your EditText

yourEditText.getTextString()

H
Himanshu itmca

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();
    }
});

s
salihzekicamurcu
    Button kapatButon = (Button) findViewById(R.id.islemButonKapat);
    Button hesaplaButon = (Button) findViewById(R.id.islemButonHesapla);
    Button ayarlarButon = (Button) findViewById(R.id.islemButonAyarlar);

    final EditText ders1Vize = (EditText) findViewById(R.id.ders1Vize);
    final EditText ders1Final = (EditText) findViewById(R.id.ders1Final);
    final EditText ders1Ortalama = (EditText) findViewById(R.id.ders1Ortalama);

    //

    final EditText ders2Vize = (EditText) findViewById(R.id.ders2Vize);
    final EditText ders2Final = (EditText) findViewById(R.id.ders2Final);
    final EditText ders2Ortalama = (EditText) findViewById(R.id.ders2Ortalama);
    //
    final EditText ders3Vize = (EditText) findViewById(R.id.ders3Vize);
    final EditText ders3Final = (EditText) findViewById(R.id.ders3Final);
    final EditText ders3Ortalama = (EditText) findViewById(R.id.ders3Ortalama);
    //
    final EditText ders4Vize = (EditText) findViewById(R.id.ders4Vize);
    final EditText ders4Final = (EditText) findViewById(R.id.ders4Final);
    final EditText ders4Ortalama = (EditText) findViewById(R.id.ders4Ortalama);
    //
    final EditText ders5Vize = (EditText) findViewById(R.id.ders5Vize);
    final EditText ders5Final = (EditText) findViewById(R.id.ders5Final);
    final EditText ders5Ortalama = (EditText) findViewById(R.id.ders5Ortalama);
    //
    final EditText ders6Vize = (EditText) findViewById(R.id.ders6Vize);
    final EditText ders6Final = (EditText) findViewById(R.id.ders6Final);
    final EditText ders6Ortalama = (EditText) findViewById(R.id.ders6Ortalama);
    //
    final EditText ders7Vize = (EditText) findViewById(R.id.ders7Vize);
    final EditText ders7Final = (EditText) findViewById(R.id.ders7Final);
    final EditText ders7Ortalama = (EditText) findViewById(R.id.ders7Ortalama);
    //

    /*
     * 
     * 
     * */

    kapatButon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // kapatma islemi
            Toast.makeText(getApplicationContext(), "kapat",
                    Toast.LENGTH_LONG).show();
        }
    });
    /*
     * 
     * 
     * */
    hesaplaButon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // hesap islemi

            int d1v = Integer.parseInt(ders1Vize.getText().toString());
            int d1f = Integer.parseInt(ders1Final.getText().toString());
            int ort1 = (int) (d1v * 0.4 + d1f * 0.6);
            ders1Ortalama.setText("" + ort1);
            //
            int d2v = Integer.parseInt(ders2Vize.getText().toString());
            int d2f = Integer.parseInt(ders2Final.getText().toString());
            int ort2 = (int) (d2v * 0.4 + d2f * 0.6);
            ders2Ortalama.setText("" + ort2);
            //
            int d3v = Integer.parseInt(ders3Vize.getText().toString());
            int d3f = Integer.parseInt(ders3Final.getText().toString());
            int ort3 = (int) (d3v * 0.4 + d3f * 0.6);
            ders3Ortalama.setText("" + ort3);
            //
            int d4v = Integer.parseInt(ders4Vize.getText().toString());
            int d4f = Integer.parseInt(ders4Final.getText().toString());
            int ort4 = (int) (d4v * 0.4 + d4f * 0.6);
            ders4Ortalama.setText("" + ort4);
            //
            int d5v = Integer.parseInt(ders5Vize.getText().toString());
            int d5f = Integer.parseInt(ders5Final.getText().toString());
            int ort5 = (int) (d5v * 0.4 + d5f * 0.6);
            ders5Ortalama.setText("" + ort5);
            //
            int d6v = Integer.parseInt(ders6Vize.getText().toString());
            int d6f = Integer.parseInt(ders6Final.getText().toString());
            int ort6 = (int) (d6v * 0.4 + d6f * 0.6);
            ders6Ortalama.setText("" + ort6);
            //
            int d7v = Integer.parseInt(ders7Vize.getText().toString());
            int d7f = Integer.parseInt(ders7Final.getText().toString());
            int ort7 = (int) (d7v * 0.4 + d7f * 0.6);
            ders7Ortalama.setText("" + ort7);
            //




            Toast.makeText(getApplicationContext(), "hesapla",
                    Toast.LENGTH_LONG).show();
        }
    });