目录
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);
}
}