获取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(); 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 = getIntent(); String value1 = intent.getStringExtra("one" ); String value2 = intent.getStringExtra("two" ); int i = Integer.parseInt(value1); int j = Integer.parseInt(value2); int value = i * j ; text3.setText(value + "" ); } }