android studio实训实例
时间: 2025-01-05 10:32:26 浏览: 12
### Android Studio 实训项目示例
#### BMI 计算器应用
此应用旨在帮助用户计算其身体质量指数 (BMI),并提供健康建议。该应用展示了如何使用 `LinearLayout` 来安排 UI 组件的位置。
```xml
<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- 输入身高 -->
<EditText
android:id="@+id/height_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入您的身高(cm)"/>
<!-- 输入体重 -->
<EditText
android:id="@+id/weight_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入您的体重(kg)"/>
<!-- 计算按钮 -->
<Button
android:id="@+id/calculate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算"/>
</LinearLayout>
```
```java
// MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText heightEditText = findViewById(R.id.height_edit_text);
final EditText weightEditText = findViewById(R.id.weight_edit_text);
Button calculateButton = findViewById(R.id.calculate_button);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String heightStr = heightEditText.getText().toString();
String weightStr = weightEditText.getText().toString();
if (!TextUtils.isEmpty(heightStr) && !TextUtils.isEmpty(weightStr)) {
float height = Float.parseFloat(heightStr) / 100;
float weight = Float.parseFloat(weightStr);
// 计算BMI
float bmiValue = weight / (height * height);
Toast.makeText(MainActivity.this, "您的BMI:" + bmiValue, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "请输入有效的数值", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
#### 类微信界面设计
此类实训项目专注于实现类似于微信的消息列表视图,重点在于理解 `RecyclerView` 的工作原理以及 `Activity` 生命周期管理。
```xml
<!-- res/layout/item_message.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<!-- 头像 -->
<ImageView
android:id="@+id/avatar_image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/default_avatar"/>
<!-- 昵称和消息内容 -->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#FF000000"
android:text="昵称"/>
<TextView
android:id="@+id/message_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#7F000000"
android:text="最新一条消息"/>
</LinearLayout>
<!-- 时间戳 -->
<TextView
android:id="@+id/time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#BDBDBD"
android:text="时间"/>
</LinearLayout>
```
```java
// MessageAdapter.java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private List<MessageItem> messageItems;
static class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView avatarImageView;
TextView nameTextView;
TextView messageTextView;
TextView timeTextView;
MessageViewHolder(@NonNull View itemView){
super(itemView);
avatarImageView = itemView.findViewById(R.id.avatar_image_view);
nameTextView = itemView.findViewById(R.id.name_text_view);
messageTextView = itemView.findViewById(R.id.message_text_view);
timeTextView = itemView.findViewById(R.id.time_text_view);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getBindingAdapterPosition();
if(position != RecyclerView.NO_POSITION){
Intent intent = new Intent(view.getContext(), ChatDetailActivity.class);
intent.putExtra("message_item", messageItems.get(position));
view.getContext().startActivity(intent);
}
}
}
@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
return new MessageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
MessageItem currentItem = messageItems.get(position);
holder.nameTextView.setText(currentItem.getName());
holder.messageTextView.setText(currentItem.getMessageContent());
holder.timeTextView.setText(currentItem.getTimeStamp());
Glide.with(holder.itemView.getContext())
.
阅读全文