Android 实现简单的手电筒(个人笔记)

这篇博客记录了如何在Android平台上实现一个简单手电筒应用的步骤,包括XML布局文件的设置,Java代码的编写以及AndroidManifest.xml中必要的权限配置。
摘要由CSDN通过智能技术生成

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Switch_Activity">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="225dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="267dp"
        app:srcCompat="@drawable/off" />

    <!-- android:layout_centerHorizontal="true" 水平居中-->

    <SurfaceView
        and
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单Android 笔记本应用的代码示例,包括增加、删除、修改和查找笔记的功能: 1. 在 activity_main.xml 中添加一个 ListView 元素和三个 Button 元素,以便用户列出、添加、编辑和删除笔记: ```xml <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add"/> <Button android:id="@+id/editButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Edit"/> <Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete"/> ``` 2. 在 note_item.xml 中添加一个 TextView 元素,以便在 ListView 中显示笔记的标题: ```xml <TextView android:id="@+id/titleTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:padding="10dp"/> ``` 3. 在 MainActivity.java 文件中编写以下代码: ```java import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; Button addButton, editButton, deleteButton; ArrayList<String> notes; ArrayAdapter<String> adapter; int selectedIndex = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); addButton = findViewById(R.id.addButton); editButton = findViewById(R.id.editButton); deleteButton = findViewById(R.id.deleteButton); notes = new ArrayList<>(); adapter = new ArrayAdapter<>(this, R.layout.note_item, R.id.titleTextView, notes); listView.setAdapter(adapter); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showNoteDialog("New note", "", true); } }); editButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedIndex != -1) { String note = notes.get(selectedIndex); showNoteDialog("Edit note", note, false); } else { Toast.makeText(MainActivity.this, "Please select a note to edit", Toast.LENGTH_SHORT).show(); } } }); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedIndex != -1) { String note = notes.get(selectedIndex); showDeleteDialog(note); } else { Toast.makeText(MainActivity.this, "Please select a note to delete", Toast.LENGTH_SHORT).show(); } } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { selectedIndex = i; } }); } private void showNoteDialog(String title, String note, final boolean isNew) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(title); final EditText input = new EditText(this); input.setText(note); builder.setView(input); builder.setPositiveButton("Save", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String text = input.getText().toString(); if (isNew) { notes.add(text); } else { notes.set(selectedIndex, text); } adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("Cancel", null); builder.show(); } private void showDeleteDialog(final String note) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Delete note"); builder.setMessage("Are you sure you want to delete this note?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { notes.remove(note); adapter.notifyDataSetChanged(); selectedIndex = -1; } }); builder.setNegativeButton("No", null); builder.show(); } } ``` 4. 运行应用程序并测试添加、编辑、删除和查找笔记的功能。 注意:这只是一个简单笔记本应用程序示例,未进行任何输入验证或错误处理。在实际应用程序中,您可能需要添加更多的代码来确保应用程序的稳定性和安全性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值