AndroidStudio记录-获取EditText的数字内容并将该内容转为int型数据

1.结果显示

未输入时的视图
输入后的视图

2.获取EditText的内容

String str1 = mTestEdt.getText().toString();

3.转换数据类型

int k = Integer.parseInt(str1);

4.显示

mShowTV.setText(getResources().getString(R.string.tv_show)+ k);

5.代码

src\main\res\values\colors.xml

    <color name="test_blue">#87CEEB</color>

src\main\res\values\strings.xml

	<string name="btn_test">test</string>
    <string name="edt_test">please input the value</string>
    <string name="tv_show">the value of input :</string>

src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edt_test"
            android:layout_width="330dp"
            android:layout_height="wrap_content"
            android:hint="@string/edt_test"
            android:textSize="18sp"/>
        <Button
            android:id="@+id/btn_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/test_blue"
            android:text="@string/btn_test"
            android:textSize="18sp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private EditText mTestEdt;
    private Button mTestBtn;
    private TextView mShowTV;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mTestBtn.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                // The following two lines of code have to be put here to be useful. Otherwise, the program will crash
                String str1 = mTestEdt.getText().toString();
                int k = Integer.parseInt(str1);
                mShowTV.setText(getResources().getString(R.string.tv_show)+ k);
            }
        });
    }

    private void initView(){
        mTestEdt = (EditText) findViewById(R.id.edt_test);
        mTestBtn = (Button) findViewById(R.id.btn_test);
        mShowTV = (TextView) findViewById(R.id.tv_show);
    }
}
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值