获取EditText控件的值:

1
String string = editText1.getText().toString() ;

demo:在一个页面做两个输入框一个按钮,填入数字,并将乘积显示在另一个页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
MainActivity代码
public class MainActivity extends AppCompatActivity {
private EditText edittext1;
private EditText edittext2;
private Button send;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext1 = (EditText)findViewById(R.id.edit_text1);
edittext2 = (EditText)findViewById(R.id.edit_text2);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取两个输入框的值
String string = edittext1.getText().toString();
String string1 = edittext2.getText().toString();

//putExtra用来传递键值
Intent intent = new Intent();
intent.putExtra("one" , string);
intent.putExtra("two" , string1);
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class OtherActivity extends AppCompatActivity{
private TextView text3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
text3 = (TextView)findViewById(R.id.text3);

//获取intent,以及键值
Intent intent = getIntent();
String value1 = intent.getStringExtra("one");
String value2 = intent.getStringExtra("two");

//因为string不能运算,所以需要将string类型转为int类型
int i = Integer.parseInt(value1); //sring类型转为int类型写法
int j = Integer.parseInt(value2);
int value = i * j ; //输入框两数字相乘

text3.setText(value + ""); //TextView只接收字符串,而value是整型,按照JAVA语法:连接符两端有一端是字符串,另外一端也会被转为字符串,所以加上了一个空字符
}
}