android:inputType参数类型说明

本文详细介绍了Android中用于不同场景的输入类型的参数配置,包括普通文本、数字、日期、时间等多种格式,以及特定用途如电子邮件、密码等的输入类型。

android:inputType参数类型说明

android:inputType参数类型说明

android:inputType="none"--输入普通字符

android:inputType="text"--输入普通字符

android:inputType="textCapCharacters"--输入普通字符

android:inputType="textCapWords"--单词首字母大

android:inputType="textCapSentences"--仅第一个字母大小

android:inputType="textAutoCorrect"--前两个自动完成

android:inputType="textAutoComplete"--前两个自动完成

android:inputType="textMultiLine"--多行输入

android:inputType="textImeMultiLine"--输入法多行(不一定支持)

android:inputType="textNoSuggestions"--不提示

android:inputType="textUri"--URI格式

android:inputType="textEmailAddress"--电子邮件地址格式

android:inputType="textEmailSubject"--邮件主题格式

android:inputType="textShortMessage"--短消息格式

android:inputType="textLongMessage"--长消息格式

android:inputType="textPersonName"--人名格式

android:inputType="textPostalAddress"--邮政格式

android:inputType="textPassword"--密码格式

android:inputType="textVisiblePassword"--密码可见格式

android:inputType="textWebEditText"--作为网页表单的文本格式

android:inputType="textFilter"--文本筛选格式

android:inputType="textPhonetic"--拼音输入格式

android:inputType="number"--数字格式

android:inputType="numberSigned"--有符号数字格式

android:inputType="numberDecimal"--可以带小数点的浮点格式

android:inputType="phone"--拨号键盘

android:inputType="datetime"

android:inputType="date"--日期键盘

android:inputType="time"--时间键盘
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 日期显示 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <TextView android:id="@+id/date_text" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="请选择日期" android:textSize="16sp"/> <Button android:id="@+id/date_picker_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择日期"/> </LinearLayout> <!-- 收支类型选择 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <Button android:id="@+id/income_button" android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/bg_gray" android:text="收入" /> <Button android:id="@+id/expense_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@android:color/holo_blue_light" android:text="支出"/> </LinearLayout> <!-- 支出类别选择图标组 --> <LinearLayout android:id="@+id/expense_category_group_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <ImageButton android:id="@+id/category_food" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_canyin_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_shopping" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_gouwu_f" /> <ImageButton android:id="@+id/category_utilities" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_shuidianfei_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_phone" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_tongxun_f" android:background="@color/button_default" /> <ImageButton android:id="@+id/category_entertainment" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_yule_f" android:background="@color/button_default" /> </LinearLayout> <LinearLayout android:id="@+id/expense_category_group_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp"> <ImageButton android:id="@+id/category_other_outcome" android:layout_width="69dp" android:layout_height="wrap_content" app:srcCompat="@mipmap/hs_qt_f" android:background="@color/button_default" /> </LinearLayout> <!-- 收入类别选择图标组 --> <LinearLayout android:id="@+id/income_category_group_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone"> <!-- 默认隐藏 --> <ImageButton android:id="@+id/category_salary" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_xinzi_f" /> <ImageButton android:id="@+id/category_collect_debt" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_shouzhai_f" /> <ImageButton android:id="@+id/category_bonus" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_jiangjin_f" /> <ImageButton android:id="@+id/category_investment" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_touzi_f" /> <ImageButton android:id="@+id/category_accident" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_yiwaisuode_f" /> </LinearLayout> <LinearLayout android:id="@+id/income_category_group_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="16dp" android:visibility="gone"> <ImageButton android:id="@+id/category_other_income" android:layout_width="69dp" android:layout_height="wrap_content" android:background="@color/button_default" app:srcCompat="@mipmap/hs_qita_f" /> </LinearLayout> <!-- 其他类别输入框(默认隐藏) --> <EditText android:id="@+id/other_category_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入类别" android:layout_marginBottom="16dp" android:visibility="gone" /> <!-- 金额输入 --> <EditText android:id="@+id/amount_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="金额" android:inputType="numberDecimal" android:layout_marginBottom="16dp"/> <!-- 提交按钮 --> <Button android:id="@+id/submit_button" android:layout_width="match_parent" android:layout_height="33dp" android:background="@color/colorPrimary" android:text="提交" android:textColor="@android:color/white" /> </LinearLayout> </ScrollView>package com.example.test_001; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.widget.TextView; import com.example.test_001.db.AccountingDBHelper; import com.example.test_001.enum_t.EnumType; import java.util.ArrayList; import java.util.Calendar; import java.util.List; public class RecordActivity extends AppCompatActivity { private ImageButton selectedImageButton = null; private TextView dateText; private Button datePickerButton; private LinearLayout expenseCategoryGroup, expenseCategoryOther; private LinearLayout incomeCategoryGroup, incomeCategoryOther; //支出按钮 private ImageButton foodButton; private ImageButton shoppingButton; private ImageButton utilitiesButton; private ImageButton phoneButton; private ImageButton otherOutcomeButton; private ImageButton entertainmentButton; //收入按钮 private ImageButton salaryButton, debtButton, bonusButton, investmentButton, accidentButton,otherIncomeButton; private EditText otherCategoryInput; private EditText amountInput; private Button submitButton; private AccountingDBHelper dbHelper; private Button incomeButton; private Button expenseButton; private EnumType selectedType = EnumType.EXPENSE; // 用于存储选中的收支类型 private int selectedYear, selectedMonth, selectedDay; private void setupImageButtonClickListener(ImageButton button) { button.setOnClickListener(v -> { if (selectedImageButton == button) { selectedImageButton.setBackgroundColor(getColor(R.color.button_default)); selectedImageButton = null; if (button.getId() == R.id.category_other_income || button.getId() == R.id.category_other_outcome) { otherCategoryInput.setVisibility(View.GONE); } } else { if (selectedImageButton != null) { selectedImageButton.setBackgroundColor(getColor(R.color.button_default)); } selectedImageButton = button; selectedImageButton.setBackgroundColor(getColor(R.color.yellow_highlight)); if (button.getId() == R.id.category_other_income || button.getId() == R.id.category_other_outcome) { otherCategoryInput.setVisibility(View.VISIBLE); } else { otherCategoryInput.setVisibility(View.GONE); } } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record); //支出 foodButton = findViewById(R.id.category_food); shoppingButton = findViewById(R.id.category_shopping); utilitiesButton = findViewById(R.id.category_utilities); phoneButton = findViewById(R.id.category_phone); otherOutcomeButton = findViewById(R.id.category_other_outcome); entertainmentButton = findViewById(R.id.category_entertainment); //收入 salaryButton = findViewById(R.id.category_salary); debtButton = findViewById(R.id.category_collect_debt); bonusButton = findViewById(R.id.category_bonus); investmentButton = findViewById(R.id.category_investment); accidentButton = findViewById(R.id.category_accident); otherIncomeButton = findViewById(R.id.category_other_income); // 初始化控件 dateText = findViewById(R.id.date_text); datePickerButton = findViewById(R.id.date_picker_button); expenseCategoryGroup = findViewById(R.id.expense_category_group_1); expenseCategoryOther = findViewById(R.id.expense_category_group_2); incomeCategoryGroup = findViewById(R.id.income_category_group_1); incomeCategoryOther = findViewById(R.id.income_category_group_2); otherCategoryInput = findViewById(R.id.other_category_input); amountInput = findViewById(R.id.amount_input); submitButton = findViewById(R.id.submit_button); // 新增按钮初始化 incomeButton = findViewById(R.id.income_button); expenseButton = findViewById(R.id.expense_button); // 初始化数据库帮助器 dbHelper = new AccountingDBHelper(this); // 设置默认日期为当前日期 final Calendar calendar = Calendar.getInstance(); selectedYear = calendar.get(Calendar.YEAR); selectedMonth = calendar.get(Calendar.MONTH); selectedDay = calendar.get(Calendar.DAY_OF_MONTH); updateDateDisplay(); // 设置日期选择器按钮点击事件 datePickerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDatePickerDialog(); } }); //因为默认是支出状态,而此时还没有进行对收入支出选项的点击,所以这里需要手动绑定支出图标的点击事件 setupImageButtonClickListener(foodButton); setupImageButtonClickListener(shoppingButton); setupImageButtonClickListener(utilitiesButton); setupImageButtonClickListener(phoneButton); setupImageButtonClickListener(entertainmentButton); setupImageButtonClickListener(otherOutcomeButton); //以下是分别把点击收入/支出这两个按钮的事件,和显示(收入/支出)、隐藏(支出/收入)图标组以及对应类别图标组的点击事件进行绑定(收入/支出) // 设置收入按钮点击事件 incomeButton.setOnClickListener(v -> { selectedType = EnumType.INCOME; incomeButton.setBackgroundColor(getColor(android.R.color.holo_red_light)); expenseButton.setBackgroundColor(getColor(R.color.bg_gray)); // 隐藏支出图标组 expenseCategoryGroup.setVisibility(View.GONE); expenseCategoryOther.setVisibility(View.GONE); // 显示收入图标组 incomeCategoryGroup.setVisibility(View.VISIBLE); incomeCategoryOther.setVisibility(View.VISIBLE); // 绑定收入图标点击事件 setupImageButtonClickListener(salaryButton); setupImageButtonClickListener(debtButton); setupImageButtonClickListener(bonusButton); setupImageButtonClickListener(investmentButton); setupImageButtonClickListener(accidentButton); setupImageButtonClickListener(otherIncomeButton); }); // 设置支出按钮点击事件 expenseButton.setOnClickListener(v -> { selectedType = EnumType.EXPENSE; expenseButton.setBackgroundColor(getColor(android.R.color.holo_blue_light)); incomeButton.setBackgroundColor(getColor(R.color.bg_gray)); // 隐藏收入图标组 incomeCategoryGroup.setVisibility(View.GONE); incomeCategoryOther.setVisibility(View.GONE); // 显示支出图标组 expenseCategoryGroup.setVisibility(View.VISIBLE); expenseCategoryOther.setVisibility(View.VISIBLE); // 恢复支出图标点击事件 setupImageButtonClickListener(foodButton); setupImageButtonClickListener(shoppingButton); setupImageButtonClickListener(utilitiesButton); setupImageButtonClickListener(phoneButton); setupImageButtonClickListener(entertainmentButton); setupImageButtonClickListener(otherOutcomeButton); }); // 设置提交按钮点击事件 submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 验证并保存数据 if (validateInputs()) { String date = dateText.getText().toString(); String type=selectedType.getValue(); String category = null; if (selectedImageButton != null) { int id = selectedImageButton.getId(); switch (id) { case R.id.category_food: category = "餐饮"; break; case R.id.category_shopping: category = "购物"; break; case R.id.category_utilities: category = "水电"; break; case R.id.category_phone: category = "话费"; break; case R.id.category_other_outcome: category = otherCategoryInput.getText().toString(); break; case R.id.category_entertainment: category = "娱乐"; break; default: category = null; break; } } float amount = Float.parseFloat(amountInput.getText().toString()); // 使用addTransaction方法插入数据到数据库 dbHelper.addTransaction(date, type, category, amount); Toast.makeText(RecordActivity.this, "记录成功", Toast.LENGTH_SHORT).show(); finish(); // 返回到主页面 } } }); } private void showDatePickerDialog() { DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, year, monthOfYear, dayOfMonth) -> { selectedYear = year; selectedMonth = monthOfYear; selectedDay = dayOfMonth; updateDateDisplay(); }, selectedYear, selectedMonth, selectedDay); datePickerDialog.show(); } private void updateDateDisplay() { String formattedMonth = (selectedMonth + 1) < 10 ? "0" + (selectedMonth + 1) : String.valueOf(selectedMonth + 1); String formattedDay = selectedDay < 10 ? "0" + selectedDay : String.valueOf(selectedDay); dateText.setText(selectedYear + "-" + formattedMonth + "-" + formattedDay); } private boolean validateInputs() { // 验证日期是否选择 if (dateText.getText().toString().isEmpty()) { Toast.makeText(this, "请选择日期", Toast.LENGTH_SHORT).show(); return false; } // 验证类别是否选择(使用 ImageButton 判断) if (selectedImageButton == null) { Toast.makeText(this, "请选择类别", Toast.LENGTH_SHORT).show(); return false; } // 如果选择了"其他"但未输入自定义类别 int id = selectedImageButton.getId(); if (id == R.id.category_other && otherCategoryInput.getText().toString().isEmpty()) { Toast.makeText(this, "请输入自定义类别", Toast.LENGTH_SHORT).show(); return false; } // 验证金额为有效数字且大于0 try { double amount = Double.parseDouble(amountInput.getText().toString()); if (amount <= 0) { Toast.makeText(this, "金额必须大于0", Toast.LENGTH_SHORT).show(); return false; } } catch (NumberFormatException e) { Toast.makeText(this, "请输入有效的金额", Toast.LENGTH_SHORT).show(); return false; } return true; } }请仿照这两个程序修改以下两个程序实现收入与支出分别和对应的五个图标绑定(把单选按钮用图标按钮替换)package com.example.test_001; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.database.Cursor; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.RadioButton; import android.app.DatePickerDialog; import com.example.test_001.db.AccountingDBHelper; import java.util.Calendar; public class SearchActivity extends AppCompatActivity { private Button backButton; private TextView searchTitle; private EditText searchInput; private Button datePickerButton; private Button incomeButton; private Button expenseButton; private RadioGroup categoryGroup; private RadioButton categoryFood; private RadioButton categoryShopping; private RadioButton categoryUtilities; private RadioButton categoryPhone; private RadioButton categoryEntertainment; private RadioButton categoryOther; private Button byAmountButton; private TextView resultTextView; private AccountingDBHelper dbHelper; private int selectedYear, selectedMonth, selectedDay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); // 初始化控件 backButton = findViewById(R.id.back_button); // 返回按钮 searchTitle = findViewById(R.id.search_title); searchInput = findViewById(R.id.search_input); datePickerButton = findViewById(R.id.date_picker_button); incomeButton = findViewById(R.id.income_button); expenseButton = findViewById(R.id.expense_button); categoryGroup = findViewById(R.id.category_group); categoryFood = findViewById(R.id.category_food); categoryShopping = findViewById(R.id.category_shopping); categoryUtilities = findViewById(R.id.category_utilities); categoryPhone = findViewById(R.id.category_phone); categoryEntertainment = findViewById(R.id.category_entertainment); categoryOther = findViewById(R.id.category_other); byAmountButton = findViewById(R.id.by_amount_button); resultTextView = findViewById(R.id.result_text_view); // 初始化数据库帮助器 dbHelper = new AccountingDBHelper(this); // 设置返回按钮点击事件 backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); // 返回到主页面 } }); // 获取当前日期作为默认选择日期 final Calendar calendar = Calendar.getInstance(); selectedYear = calendar.get(Calendar.YEAR); selectedMonth = calendar.get(Calendar.MONTH); selectedDay = calendar.get(Calendar.DAY_OF_MONTH); // 设置日期选择器按钮点击事件 datePickerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDatePickerDialog(); } }); // 设置收入按钮点击事件 incomeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Cursor cursor = dbHelper.getReadableDatabase().query( AccountingDBHelper.TABLE_TRANSACTIONS, new String[]{AccountingDBHelper.COLUMN_DATE, AccountingDBHelper.COLUMN_TYPE, AccountingDBHelper.COLUMN_CATEGORY, AccountingDBHelper.COLUMN_AMOUNT}, AccountingDBHelper.COLUMN_TYPE + "=?", // 查询条件 new String[]{"income"}, // 使用英文字段值进行查询 null, null, AccountingDBHelper.COLUMN_DATE + " ASC"); // 按日期升序排列 displayResults(cursor); } }); // 设置支出按钮点击事件 expenseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Cursor cursor = dbHelper.getReadableDatabase().query( AccountingDBHelper.TABLE_TRANSACTIONS, new String[]{AccountingDBHelper.COLUMN_DATE, AccountingDBHelper.COLUMN_TYPE, AccountingDBHelper.COLUMN_CATEGORY, AccountingDBHelper.COLUMN_AMOUNT}, AccountingDBHelper.COLUMN_TYPE + "=?", // 查询条件 new String[]{"expense"}, // 使用英文字段值进行查询 null, null, AccountingDBHelper.COLUMN_DATE + " ASC"); // 按日期升序排列 displayResults(cursor); } }); // 设置类别单选组的监听器 categoryGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { String category = null; // 确定选择了哪个类别 switch (checkedId) { case R.id.category_food: category = "餐饮"; break; case R.id.category_shopping: category = "购物"; break; case R.id.category_utilities: category = "水电"; break; case R.id.category_phone: category = "话费"; break; case R.id.category_entertainment: category = "娱乐"; break; case R.id.category_other: category = "其他"; break; } if (category != null) { Cursor cursor = dbHelper.getReadableDatabase().query( AccountingDBHelper.TABLE_TRANSACTIONS, new String[]{AccountingDBHelper.COLUMN_DATE, AccountingDBHelper.COLUMN_TYPE, AccountingDBHelper.COLUMN_CATEGORY, AccountingDBHelper.COLUMN_AMOUNT}, AccountingDBHelper.COLUMN_CATEGORY + "=?", // 查询条件 new String[]{category}, // 查询条件参数 null, null, AccountingDBHelper.COLUMN_DATE + " ASC"); // 按日期升序排列 displayResults(cursor); } } }); // 设置按金额查找按钮点击事件 byAmountButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String amountStr = searchInput.getText().toString(); if (!amountStr.isEmpty()) { try { double amount = Double.parseDouble(amountStr); Cursor cursor = dbHelper.getReadableDatabase().query( AccountingDBHelper.TABLE_TRANSACTIONS, new String[]{AccountingDBHelper.COLUMN_DATE, AccountingDBHelper.COLUMN_TYPE, AccountingDBHelper.COLUMN_CATEGORY, AccountingDBHelper.COLUMN_AMOUNT}, AccountingDBHelper.COLUMN_AMOUNT + "=?", // 查询条件 new String[]{String.valueOf(amount)}, // 查询条件参数 null, null, null); displayResults(cursor); } catch (NumberFormatException e) { Toast.makeText(SearchActivity.this, "请输入有效的金额", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(SearchActivity.this, "请输入金额进行搜索", Toast.LENGTH_SHORT).show(); } } }); } private void showDatePickerDialog() { DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view, year, monthOfYear, dayOfMonth) -> { selectedYear = year; selectedMonth = monthOfYear; selectedDay = dayOfMonth; updateDateDisplay(); }, selectedYear, selectedMonth, selectedDay); // 获取当前日期的时间戳 long currentMillis = System.currentTimeMillis(); datePickerDialog.getDatePicker().setMaxDate(currentMillis); // 限制最大日期为今天 datePickerDialog.show(); } private void updateDateDisplay() { String formattedMonth = (selectedMonth + 1) < 10 ? "0" + (selectedMonth + 1) : String.valueOf(selectedMonth + 1); String formattedDay = selectedDay < 10 ? "0" + selectedDay : String.valueOf(selectedDay); String date = selectedYear + "-" + formattedMonth + "-" + formattedDay; Cursor cursor = dbHelper.getReadableDatabase().query( AccountingDBHelper.TABLE_TRANSACTIONS, new String[]{AccountingDBHelper.COLUMN_DATE, AccountingDBHelper.COLUMN_TYPE, AccountingDBHelper.COLUMN_CATEGORY, AccountingDBHelper.COLUMN_AMOUNT}, AccountingDBHelper.COLUMN_DATE + "=?", // 查询条件 new String[]{date}, // 查询条件参数 null, null, null); displayResults(cursor); } private void displayResults(Cursor cursor) { if (cursor.getCount() == 0) { Toast.makeText(this, "没有找到匹配的数据", Toast.LENGTH_SHORT).show(); return; } StringBuilder result = new StringBuilder(); while (cursor.moveToNext()) { String date = cursor.getString(cursor.getColumnIndexOrThrow(AccountingDBHelper.COLUMN_DATE)); String type = cursor.getString(cursor.getColumnIndexOrThrow(AccountingDBHelper.COLUMN_TYPE)); String category = cursor.getString(cursor.getColumnIndexOrThrow(AccountingDBHelper.COLUMN_CATEGORY)); double amount = cursor.getDouble(cursor.getColumnIndexOrThrow(AccountingDBHelper.COLUMN_AMOUNT)); result.append("日期: ").append(date) .append(", 类型: ").append(type) .append(", 类别: ").append(category) .append(", 金额: ").append(amount) .append("\n"); } // 显示结果 resultTextView.setText(result.toString()); } }<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".SearchActivity"> <!-- 返回按钮 --> <Button android:id="@+id/back_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:layout_alignParentStart="true" android:layout_marginTop="16dp" android:layout_marginStart="16dp"/> <!-- 查找标题 --> <TextView android:id="@+id/search_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查找页面" android:textSize="20sp" android:textStyle="bold" android:layout_centerHorizontal="true" android:layout_marginTop="16dp"/> <!-- 搜索框与日历按钮 --> <LinearLayout android:id="@+id/search_input_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/search_title" android:layout_marginTop="16dp"> <EditText android:id="@+id/search_input" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="输入搜索内容" android:inputType="text" android:layout_marginRight="8dp"/> <Button android:id="@+id/date_picker_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="📅" android:textSize="20sp"/> </LinearLayout> <!-- 查找按钮区域 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_below="@id/search_input_container" android:layout_marginTop="20dp"> <!-- 收入/支出按钮 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="10dp"> <Button android:id="@+id/income_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@color/bg_gray" android:text="收入"/> <Button android:id="@+id/expense_button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@color/bg_gray" android:text="支出"/> </LinearLayout> <!-- 类别单选组 --> <RadioGroup android:id="@+id/category_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="10dp"> <RadioButton android:id="@+id/category_food" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="餐饮"/> <RadioButton android:id="@+id/category_shopping" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="购物"/> <RadioButton android:id="@+id/category_utilities" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水电"/> <RadioButton android:id="@+id/category_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="话费"/> <RadioButton android:id="@+id/category_entertainment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="娱乐"/> <RadioButton android:id="@+id/category_other" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他"/> </RadioGroup> <!-- 按金额查找按钮 --> <Button android:id="@+id/by_amount_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按金额查找"/> <!-- 查询结果显示区域 --> <TextView android:id="@+id/result_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/by_amount_button" android:layout_marginTop="20dp" android:textSize="16sp"/> </LinearLayout> </RelativeLayout>
06-28
说明使用了哪些控件,使用的属性有哪些 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/register_bg"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="50dp" android:background="#01ceff" android:gravity="center" android:text="注册" android:textColor="@android:color/white" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="130dp" android:orientation="horizontal"> <TextView style="@style/tvOne" android:drawableTop="@drawable/qq_icon" android:text="用QQ注册" /> <View style="@style/vLine" /> <TextView style="@style/tvOne" android:drawableTop="@drawable/weixin_icon" android:text="用微信注册" /> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:padding="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/email_icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="使用电子邮箱注册" android:textColor="@android:color/white" android:textSize="15sp" /> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="15dp"> <TextView style="@style/tvTwo" android:text="名字" /> <EditText android:id="@+id/et_name" style="@style/etOne" /> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="15dp"> <TextView style="@style/tvTwo" android:text="邮箱" /> <EditText android:id="@+id/et_email" style="@style/etOne" /> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="15dp"> <TextView style="@style/tvTwo" android:text="密码" /> <EditText android:id="@+id/et_pwd" style="@style/etOne" android:inputType="textPassword" /> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="15dp"> <TextView style="@style/tvTwo" android:text="性别" /> <RadioGroup android:id="@+id/rg_sex" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="50dp" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_boy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="@android:color/white" android:textSize="15sp" /> <RadioButton android:id="@+id/rb_girl" style="@style/tvTwo" android:text="女"/> </RadioGroup> </LinearLayout> <View style="@style/hLine" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择兴趣爱好:" android:textColor="@android:color/white" android:textSize="15sp" /> <CheckBox android:id="@+id/cb_sing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="唱歌" android:textColor="@android:color/white" android:textSize="15sp" /> <CheckBox android:id="@+id/cb_dance" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳舞" android:textColor="@android:color/white" android:textSize="15sp" /> <CheckBox android:id="@+id/cb_read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读书" android:textColor="@android:color/white" android:textSize="15sp" /> </LinearLayout> <View style="@style/hLine" /> </LinearLayout> <View android:id="@+id/v_line" android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@id/btn_submit" android:background="@android:color/darker_gray" /> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:gravity="center" android:text="提交" android:textColor="@android:color/white" android:background="@null" android:textSize="18sp" /> </RelativeLayout> package com.example.project3; import android.annotation.SuppressLint; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; // AndroidX 等效包 import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { private EditText et_name, et_email, et_pwd; private Button btn_submit; private String name, email, pwd, sex, hobbys; private RadioGroup rg_sex; private CheckBox cb_sing, cb_dance, cb_read; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { //获取界面控件 et_name = findViewById(R.id.et_name); et_email = findViewById(R.id.et_email); et_pwd = findViewById(R.id.et_pwd); rg_sex = findViewById(R.id.rg_sex); cb_sing = findViewById(R.id.cb_sing); cb_dance = findViewById(R.id.cb_dance); cb_read = findViewById(R.id.cb_read); btn_submit = findViewById(R.id.btn_submit); btn_submit.setOnClickListener(this);//设置提交按钮的点击事件的监听器 //设置复选框控件的点击事件的监听器 cb_sing.setOnCheckedChangeListener(this); cb_dance.setOnCheckedChangeListener(this); cb_read.setOnCheckedChangeListener(this); hobbys = new String(); //设置单选按钮的点击事件 rg_sex.setOnCheckedChangeListener(new RadioGroup. OnCheckedChangeListener() { @SuppressLint("NonConstantResourceId") @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.rb_boy)//判断被点击的RadioButton sex = "男"; if (checkedId == R.id.rb_girl) sex = "女"; } }); } /** * 获取界面输入的信息 */ private void getData() { name = et_name.getText().toString().trim(); email = et_email.getText().toString().trim(); pwd = et_pwd.getText().toString().trim(); } @Override public void onClick(View v) { if (v.getId() == R.id.btn_submit) { //提交按钮的点击事件 getData(); if (TextUtils.isEmpty(name)) { Toast.makeText(MainActivity.this, "请输入名字", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(email)) { Toast.makeText(MainActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(pwd)) { Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(sex)) { Toast.makeText(MainActivity.this, "请选择性别", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(hobbys)) { Toast.makeText(MainActivity.this, "请选择兴趣爱好", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); Log.i("MainActivity", "注册的用户信息:" + "名字:" + name + ", 邮箱:" + email + ", 性别:" + sex + ", 兴趣爱好:" + hobbys); } } } /** * 复选框的点击事件 */ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String motion = buttonView.getText().toString();//获取复选框中的内容 if (isChecked) { if (!hobbys.contains(motion)) { //判断之前选择的内容是否与此次选择的不一样 hobbys = hobbys + motion; } } else { if (hobbys.contains(motion)) { hobbys = hobbys.replace(motion, ""); } } } }
最新发布
11-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值