1、从一个Activity跳转到另一个Activity,使用Intent.
在按钮的onClick中如下写法:
public void Login(View view) { Intent intent = new Intent(); //设置从Relativeactivity跳转到LoginActivity intent.setClass(Relativeactivity.this, LoginActivity.class); //用putExtra来从一个Activity向另一个来传递数据 intent.putExtra("name", "独此一家"); intent.putExtra("password", "1234"); startActivity(intent); }
在新打开的Activity中进行接收
private EditText loginNameEditText = null; private EditText loginPasswordEditText = null; protected void onCreate(Bundle savedInstanceBundle) { super.onCreate(savedInstanceBundle); setContentView(layout.login); //requestWindowFeature(Window.FEATURE_NO_TITLE); loginNameEditText = (EditText)findViewById(R.id.LoginName); loginPasswordEditText = (EditText)findViewById(R.id.LoginPassword); //接到传递过来的Intent Intent intent = getIntent(); //接收Intent中的数据 String nameString = intent.getStringExtra("name"); String passwordString = intent.getStringExtra("password"); loginNameEditText.setText(nameString); loginPasswordEditText.setText(passwordString); }
2、判断文本框是否为空,并弹出提示 ,参考链接:http://zhouhaitao.iteye.com/blog/1831523
public void Login(View view) { String loginNameString = loginNameEditText.getText().toString(); String loginPasswordString = loginPasswordEditText.getText().toString(); Log.d("LoginActivity", loginNameString); //判断是否为空用equals if(loginNameString.equals("")) { //这个提示是显示一下自动就消失了 Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; } else { Toast.makeText(LoginActivity.this, "已经有了", Toast.LENGTH_SHORT).show(); } if(loginPasswordString.equals("")) { //这个是alert提示框,可以设置 标题、内容和按钮,不要忘了最后的show new AlertDialog.Builder(view.getContext()).setTitle("提示").setMessage("密码不能为空!") .setPositiveButton("确定", null).show(); } }
时间: 2024-11-10 11:19:50