Activity组件跳转和数据传递技术
时间: 2023-10-28 20:35:10 浏览: 95
Activity组件的跳转和数据传递技术主要包括以下几种方式:
1. 使用Intent进行跳转和传递数据。Intent是Android中用于在不同组件之间传递数据的一种机制,可以通过putExtra()方法来传递数据,通过startActivity()方法来启动另一个Activity组件。
2. 使用Bundle进行数据传递。Bundle是一种用于在Activity之间传递数据的容器,可以通过putXXX()方法来存储数据,通过getXXX()方法来获取数据。
3. 使用静态变量进行数据传递。在一个Activity中定义一个静态变量,然后在另一个Activity中直接访问该变量来获取数据。
4. 使用SharedPreferences进行数据传递。SharedPreferences是一种用于在不同组件之间共享数据的机制,可以通过putXXX()方法来存储数据,通过getXXX()方法来获取数据。
5. 使用Application进行数据传递。Application是一个全局的单例对象,可以在其中定义一些全局变量来存储数据,然后在不同的Activity中访问这些变量来获取数据。
相关问题
一个App综合项目(结合所学知识和案例),主要包括下面内容:ꔷ 注册,登录(SQLite技术),ꔷ 记住密码(数据存储技术)ꔷ 购买武器游戏(Activity组件跳转和数据传递技术)程序代码
好的,下面是一个简单的App综合项目代码,包括注册、登录、记住密码和购买武器游戏功能。由于篇幅限制,我只提供核心代码,如有需要可以自行补充。
1. 注册和登录功能实现(使用SQLite技术)
注册页面代码:
```
public class RegisterActivity extends AppCompatActivity {
EditText etUsername, etPassword, etConfirmPassword;
Button btnRegister;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
etConfirmPassword = findViewById(R.id.et_confirm_password);
btnRegister = findViewById(R.id.btn_register);
dbHelper = new DBHelper(this);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String confirmPassword = etConfirmPassword.getText().toString();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {
Toast.makeText(RegisterActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
return;
}
if (!password.equals(confirmPassword)) {
Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
return;
}
if (dbHelper.checkUser(username)) {
Toast.makeText(RegisterActivity.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
return;
}
dbHelper.addUser(username, password);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
```
登录页面代码:
```
public class LoginActivity extends AppCompatActivity {
EditText etUsername, etPassword;
Button btnLogin;
CheckBox cbRemember;
DBHelper dbHelper;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.et_username);
etPassword = findViewById(R.id.et_password);
btnLogin = findViewById(R.id.btn_login);
cbRemember = findViewById(R.id.cb_remember);
dbHelper = new DBHelper(this);
sp = getSharedPreferences("user", MODE_PRIVATE);
if (sp.getBoolean("remember_password", false)) {
etUsername.setText(sp.getString("username", ""));
etPassword.setText(sp.getString("password", ""));
cbRemember.setChecked(true);
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
return;
}
if (!dbHelper.checkUser(username, password)) {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
return;
}
if (cbRemember.isChecked()) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("remember_password", true);
editor.apply();
} else {
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.apply();
}
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
});
}
}
```
2. 记住密码功能实现(使用SharedPreferences技术)
在登录页面的onCreate()方法中,我们使用SharedPreferences读取保存的用户名和密码,并将它们自动填充到输入框中:
```
sp = getSharedPreferences("user", MODE_PRIVATE);
if (sp.getBoolean("remember_password", false)) {
etUsername.setText(sp.getString("username", ""));
etPassword.setText(sp.getString("password", ""));
cbRemember.setChecked(true);
}
```
在登录成功后,如果用户勾选了记住密码,我们使用SharedPreferences保存用户名和密码:
```
if (cbRemember.isChecked()) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.putBoolean("remember_password", true);
editor.apply();
} else {
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.apply();
}
```
3. 购买武器游戏功能实现(使用Activity组件跳转和数据传递技术)
商品列表页面代码:
```
public class WeaponListActivity extends AppCompatActivity {
ListView lvWeapon;
List<Weapon> weaponList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weapon_list);
lvWeapon = findViewById(R.id.lv_weapon);
weaponList = new ArrayList<>();
weaponList.add(new Weapon("AK47", 3000));
weaponList.add(new Weapon("M4A1", 4000));
weaponList.add(new Weapon("AWP", 5000));
WeaponAdapter adapter = new WeaponAdapter(this, weaponList);
lvWeapon.setAdapter(adapter);
lvWeapon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Weapon weapon = weaponList.get(position);
Intent intent = new Intent(WeaponListActivity.this, WeaponDetailActivity.class);
intent.putExtra("name", weapon.getName());
intent.putExtra("price", weapon.getPrice());
startActivity(intent);
}
});
}
}
```
购物车页面代码:
```
public class ShoppingCartActivity extends AppCompatActivity {
TextView tvTotalPrice;
Button btnCheckout;
List<Weapon> weaponList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
tvTotalPrice = findViewById(R.id.tv_total_price);
btnCheckout = findViewById(R.id.btn_checkout);
weaponList = new ArrayList<>();
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int price = intent.getIntExtra("price", 0);
weaponList.add(new Weapon(name, price));
int totalPrice = 0;
for (Weapon weapon : weaponList) {
totalPrice += weapon.getPrice();
}
tvTotalPrice.setText("总价:" + totalPrice);
btnCheckout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ShoppingCartActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
```
在商品列表页面中,我们使用ListView来显示商品列表。当用户点击某个商品时,我们使用Intent来传递商品的名称和价格到购物车页面:
```
lvWeapon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Weapon weapon = weaponList.get(position);
Intent intent = new Intent(WeaponListActivity.this, WeaponDetailActivity.class);
intent.putExtra("name", weapon.getName());
intent.putExtra("price", weapon.getPrice());
startActivity(intent);
}
});
```
在购物车页面中,我们使用Intent来接收商品的名称和价格,并将它们添加到购物车中。同时,我们还计算了购物车中所有商品的总价,并显示在页面上。
4. 数据库操作类DBHelper代码
```
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "user.db";
private static final int DB_VERSION = 1;
private static final String TABLE_USER = "user";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_USER + " (" +
COLUMN_USERNAME + " TEXT PRIMARY KEY," +
COLUMN_PASSWORD + " TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: 升级数据库时的操作
}
public void addUser(String username, String password) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PASSWORD, password);
db.insert(TABLE_USER, null, values);
db.close();
}
public boolean checkUser(String username) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_USER, null, COLUMN_USERNAME + "=?", new String[]{username}, null, null, null);
boolean exist = cursor.getCount() > 0;
cursor.close();
db.close();
return exist;
}
public boolean checkUser(String username, String password) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_USER, null, COLUMN_USERNAME + "=? AND " + COLUMN_PASSWORD + "=?", new String[]{username, password}, null, null, null);
boolean exist = cursor.getCount() > 0;
cursor.close();
db.close();
return exist;
}
}
```
这是一个简单的DBHelper类,用来创建和升级数据库,并提供了添加用户和检查用户的方法。在实际项目中,你可能需要更加完善的数据库操作。
阅读全文