用java代码 编写一个安卓app 功能包含 添加候选人 进行投票 显示结果

时间: 2024-04-20 11:24:38 浏览: 21
当然,我可以为你提供一个基本的Android app代码示例,该示例具有添加候选人、进行投票和显示结果的功能。以下是一个基本的投票Android app的代码框架: 首先,在`MainActivity.java`文件中,我们将处理应用程序的主要逻辑和用户界面: ```java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity { private EditText candidateNameEditText; private Button addCandidateButton; private EditText voteEditText; private Button voteButton; private TextView resultTextView; private Map<String, Integer> candidates; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); candidateNameEditText = findViewById(R.id.editTextCandidate); addCandidateButton = findViewById(R.id.buttonAddCandidate); voteEditText = findViewById(R.id.editTextVote); voteButton = findViewById(R.id.buttonVote); resultTextView = findViewById(R.id.textViewResult); candidates = new HashMap<>(); addCandidateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String candidateName = candidateNameEditText.getText().toString(); addCandidate(candidateName); candidateNameEditText.setText(""); } }); voteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String candidateName = voteEditText.getText().toString(); voteForCandidate(candidateName); voteEditText.setText(""); } }); } public void addCandidate(String candidateName) { if (!candidateName.isEmpty()) { candidates.put(candidateName, 0); resultTextView.setText(""); } } public void voteForCandidate(String candidateName) { if (!candidateName.isEmpty()) { if (candidates.containsKey(candidateName)) { int currentVotes = candidates.get(candidateName); candidates.put(candidateName, currentVotes + 1); resultTextView.setText(""); } else { resultTextView.setText("Invalid candidate!"); } } } public void displayResults(View view) { StringBuilder result = new StringBuilder(); for (Map.Entry<String, Integer> entry : candidates.entrySet()) { result.append(entry.getKey()).append(": ").append(entry.getValue()).append(" votes\n"); } resultTextView.setText(result.toString()); } } ``` 然后,在`activity_main.xml`文件中,我们可以设置应用程序的用户界面布局。这是一个简单的示例布局,你可以根据自己的需求进行更改和扩展: ```xml <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:paddingLeft="16dp" android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp" tools:context=".MainActivity"> <TextView android:id="@+id/textViewCandidate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Candidate Name:" android:layout_marginTop="16dp"/> <EditText android:id="@+id/editTextCandidate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/textViewCandidate" android:layout_marginTop="16dp"/> <Button android:id="@+id/buttonAddCandidate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/editTextCandidate" android:layout_marginTop="16dp" android:text="Add Candidate"/> <TextView android:id="@+id/textViewVote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Vote for Candidate:" android:layout_below="@id/buttonAddCandidate" android:layout_marginTop="16dp"/> <EditText android:id="@+id/editTextVote" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/textViewVote" android:layout_below="@id/buttonAddCandidate" android:layout_marginTop="16dp"/> <Button android:id="@+id/buttonVote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Vote" android:layout_below="@id/editTextVote" android:layout_marginTop="16dp"/> <Button android:id="@+id/buttonDisplayResults" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display Results" android:onClick="displayResults" android:layout_below="@id/buttonVote" android:layout_marginTop="16dp"/> <TextView android:id="@+id/textViewResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@android:color/black" android:layout_below="@id/buttonDisplayResults" android:layout_marginTop="16dp"/> </RelativeLayout> ``` 以上代码提供了一个基本的投票Android app,它包含添加候选人、进行投票和显示结果的功能。你可以根据自己的需求进行进一步的定制和扩展。希望这对你有所帮助!如果你有任何其他问题,请随时提问。

最新推荐

recommend-type

ANDROID 完美退出APP的实例代码

介绍了ANDROID 完美退出APP的实例代码,有需要的朋友可以参考一下
recommend-type

Android studio编写简单的手电筒APP

一个简单的APP控制的手电筒代码,android studio编写一个手电筒app,调用手机的闪光等实现手电筒的功能,感兴趣的小伙伴们可以参考一下
recommend-type

Android studio创建第一个app

主要为大家详细介绍了如何使用Android studio创建你的第一个项目Hello World,感兴趣的小伙伴们可以参考一下
recommend-type

Android App启动图启动界面(Splash)的简单实现代码

主要介绍了Android App启动图启动界面(Splash)的简单实现代码,本文通过实例图文详解相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

详解APP微信支付(java后台_统一下单和回调)

主要介绍了APP微信支付(java后台_统一下单和回调),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。