获取编辑文本字段的值

发布于 2024-10-09 07:57:09 字数 183 浏览 0 评论 0原文

我正在学习如何创建 UI 元素。我创建了一些 EditText 输入字段。单击按钮时,我想捕获输入到该输入字段中的内容。

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

那是我的领域。我怎样才能获取内容?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

非要怀念 2024-10-16 07:57:09

通过使用 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());
            }
        });
}

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());
            }
        });
}
乱世争霸 2024-10-16 07:57:09

从 android 中的 EditText 控件获取值。 EditText getText 属性用于获取 EditText 的值:

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

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();
哭泣的笑容 2024-10-16 07:57:09

我想您在调用 EditText 对象的“mEdit”时必须使用此代码:

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

只需确保编译器知道哪个 EditText 来调用和使用。

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.

若水般的淡然安静女子 2024-10-16 07:57:09

我希望这个能起作用:

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

我尝试了 Integer.getInteger() 方法而不是 valueOf() - 它不起作用。

I hope this one should work:

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

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

再可℃爱ぅ一点好了 2024-10-16 07:57:09
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());
            }
        });
}
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());
            }
        });
}
抚笙 2024-10-16 07:57:09

您可能还想看看黄油刀。它的目的是通过使用注释来减少样板代码的数量。这是一个简单的示例:

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

只需将以下依赖项添加到您的 build.gradle 中:

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

作为替代方案,还有 AndroidAnnotations

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.

烦人精 2024-10-16 07:57:09

最短&最简单

getText(editText);

getText(button);

getText(textView);

小解决方法

只需在 BaseActivity 中创建方法/创建 BaseActivity(如果没有)。

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

并通过此 BaseActivity 扩展您的所有活动。

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

请注意,EditTextButton 扩展了 TextView,因此我仅创建了 getText(TextView tv)

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

寂寞笑我太脆弱 2024-10-16 07:57:09

字符串值 = YourEditText.getText().toString;

String value = YourEditText.getText().toString;

浅忆 2024-10-16 07:57:09

更高级的方法是使用 Butterknife BindView。这消除了冗余代码。

在你的 gradle 的依赖项下;添加这2行。

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

然后同步。
MainActivity 中绑定 edittext 的示例

import butterknife.BindView;   
import butterknife.ButterKnife; 

public class MainActivity {

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

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

}

但是,一旦您感觉更舒服或开始处理大量数据,这就是一个替代方案。

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.

紫南 2024-10-16 07:57:09

步骤 1:创建名称为 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>

步骤 2:创建类 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);
            }
        });
 }
}

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);
            }
        });
 }
}
听,心雨的声音 2024-10-16 07:57:09

如果您使用 Kotlin 并希望使其通用,您可以使用扩展函数 Extension function

:

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

并直接从 EditText 调用此方法

yourEditText.getTextString()

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()
冷月断魂刀 2024-10-16 07:57:09

试试这个代码

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

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();
    }
});
岛歌少女 2024-10-16 07:57:09
    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();
        }
    });
    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();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文