Android点击事件、下拉菜单、单选框实例
Android点击事件、下拉菜单、单选框实例
点击事件
1.ClickActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;public class ClickActivity extends Activity {private Button bt1;private Button bt2;private TextView tv1;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_click);bt1=(Button) findViewById(R.id.bt1);bt1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {tv1=(TextView) findViewById(R.id.test);tv1.setText("这是监听事件");}});}// @Override
// public void onClick(View v){// }public void myClick(View v){tv1=(TextView) findViewById(R.id.test);tv1.setText("这是函数点击事件");}
}
2.activity_click.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="几种点击事件的设置方法"android:gravity="center"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="---------测试文本-----------"android:gravity="center"/><TextViewandroid:id="@+id/test"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/bt1"android:text="通过监听器"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/bt2"android:text="通过设置额外函数"android:onClick="myClick"/></LinearLayout>
效果图:
单选框
1.RadioGroupActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;public class RadioGroupActivity extends Activity {private RadioGroup sex;private RadioButton man;private RadioButton woman;private RadioButton dog;private TextView text;@Overridepublic void onCreate(Bundle saveInstanceState){super.onCreate(saveInstanceState);super.setContentView(R.layout.activity_radio);sex=(RadioGroup) findViewById(R.id.sex);man=(RadioButton)findViewById(R.id.man);woman=(RadioButton)findViewById(R.id.woman);dog=(RadioButton)findViewById(R.id.dog);sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(RadioGroup group,int checkedId){RadioButton ch=(RadioButton)findViewById(checkedId);text=findViewById(R.id.text);if(ch==man){text.setText("男人");}else if (ch==woman){text.setText("女人");}else{text.setText("狗");}}});}
2.activity_radio
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/text"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="请选择你的性别"/><RadioGroupandroid:id="@+id/sex"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:checkedButton="@id/dog"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/man"android:text="男人"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/woman"android:text="女人"/><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/dog"android:text="狗"/></RadioGroup>
效果图:
下拉菜单
1.SpinnerActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class SpinnerActivity extends Activity {private Spinner Scolor;private Spinner Scity;private Spinner Sfruit;private Spinner Sanimal;private TextView demo;List<CharSequence> dataAnimal;private ArrayAdapter<CharSequence> adapterFruit;private ArrayAdapter<CharSequence> adapterAnimal;@Overridepublic void onCreate(Bundle saveInstanceStates){super.onCreate(saveInstanceStates);super.setContentView(R.layout.activity_spinner);this.Scolor=findViewById(R.id.color);this.Scity=findViewById(R.id.city);this.Sfruit=findViewById(R.id.fruit);this.Sanimal=findViewById(R.id.animal);this.demo=findViewById(R.id.demo);this.Scity.setPrompt("选城市");this.Scolor.setPrompt("选颜色");this.Sfruit.setPrompt("选水果");this.Sanimal.setPrompt("选动物");// 设置之后不起作用:prompt属性只有在dialog状态才有用,所以要在xml中,将style设置为Widget.Spinner
// prompt属性要用string下资源,不支持字符直接输入,否则会报错误this.adapterFruit=ArrayAdapter.createFromResource(this,R.array.fruit_label,android.R.layout.simple_spinner_item);this.adapterFruit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);this.Sfruit.setAdapter(adapterFruit);this.dataAnimal=new ArrayList<CharSequence>();this.dataAnimal.add("柴犬");this.dataAnimal.add("猫");this.dataAnimal.add("大象");this.dataAnimal.add("蟒蛇");this.dataAnimal.add("犀牛");this.adapterAnimal=new ArrayAdapter(this,android.R.layout.simple_spinner_item,this.dataAnimal);this.adapterAnimal.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);this.Sanimal.setAdapter(this.adapterAnimal);this.Scity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i,long l){String info=adapterView.getItemAtPosition(i).toString();//获得i所在的文本demo.setText(info);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});this.Scolor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i,long l){String info=adapterView.getItemAtPosition(i).toString();//获得i所在的文本switch (info){case "红色":demo.setTextColor(0xFF0000FF);break;case "黑色":demo.setTextColor(0xFFFFFFFF);break;case "黄色":demo.setTextColor(0xADFFA0FF);break;case "蓝色":demo.setTextColor(0x7B68EEFF);break;case "绿色":demo.setTextColor(0x7CFC00FF);break;}}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});}
}
2.activity_spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/demo"android:text="测试样例"android:gravity="center"android:textSize="80px"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择你喜欢的城市"/><Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/city"android:entries="@array/city_labels" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择你喜欢的城市颜色"/><Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/color"android:entries="@array/color_labels" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择你喜欢的水果"/><Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/fruit"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="选择你喜欢的动物"/><Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/animal"/></LinearLayout>
效果图:
项目结构
本篇文章,仅做本人的Android学习路程记录作用,如果能帮到各位再好不过,若有错误也希望大佬们指出!!!
Android点击事件、下拉菜单、单选框实例相关推荐
- android二级菜单实现,Android编程实现二级下拉菜单及快速搜索的方法
本文实例讲述了Android编程实现二级下拉菜单及快速搜索的方法.分享给大家供大家参考,具体如下: 一.我们要做什么? 上面有个搜索框,下面是一个二级下拉菜单. 输入查询内容,下面列表将显示查询结果. ...
- excel添加列下拉框票价_excel表格下拉表格添加数据-excel2017表格中怎么制作下拉菜单列表框...
在Excel表中,如何将增加下拉菜单的选项? excel中的下拉菜单选项,就是筛选的功能,具体操作如下: 1.首先选中a.b两列数据,在"开始"选项卡上选择"筛选&quo ...
- Android实现可编辑下拉菜单
Android实现仿QQ登录可编辑下拉菜单 在Android里,直接提供的Spinner控件虽然可以实现下拉菜单的效果,但其效果并不理想,很多时候我们需要类似手机QQ那样既可以在文本框中直接输入编辑文 ...
- android 美团下拉菜单,Android仿美团分类下拉菜单实例代码
本文实例为大家分享了Android仿美团下拉菜单的实现代码,分类进行选择,供大家参考,具体内容如下 效果图 操作平台 AS2.0 第三方框架:butterknife build.gradle depe ...
- android的dropdownmenu,Bootstrap 下拉菜单.dropdown的具体使用方法
本章将具体讲解下拉菜单的交互.使用下拉菜单(Dropdown)插件,您可以向任何组件(比如导航栏.标签页.胶囊式导航菜单.按钮等)添加下拉菜单. 下拉菜单.dropdown具体用法 .dropdown ...
- 如何设计下拉菜单(技巧+实例)
下拉菜单可以说是网页设计中令人又爱又恨的元素之一了.下拉菜单有许多优点:不占地方,不需要做输入验证,所有平台都支持,技术门槛低,用户都很熟悉其使用方法.然而与之同时,下拉菜单又是最容易被错误使用的表单 ...
- Extjs grid禁用头部点击三角下拉菜单
表格头部的三角在点击的时候禁止出现下拉菜单,给每一列添加属性menuDisabled:true xtype:'grid', enableColumnResize:false, columns:[ {t ...
- 关于VB.NET 菜单栏ToolStripMenu 下拉菜单单选功能的实现
首先定义一个方法: 1 Private Sub SingleCheck(ByVal sender As Object) 2 RawDataToolStripMenuItem.Checked = Fal ...
- html设置下拉菜单文本框隐藏,下拉选择框onchange触发隐藏输入框
下拉选择框onchange触发隐藏输入框 window.οnlοad=function(){ var sel=document.getElementById("sel"); if ...
- 安卓游戏时禁止状态栏下拉,如何在Android中禁用通知栏下拉菜单?
I am building a new lock screen for Android, but I am unable to lock the notification bar from pulli ...
最新文章
- codebilcks怎么新建c++文件_赤峰将新建1280个公交候车亭丨明年,将对这6条街路实施改造|雨污分流|赤峰|候车亭|红山区|泵站...
- 读取缓存行的伪共享问题
- 卡尔曼滤波、粒子滤波【通俗解释】
- 更改Tomcat虚拟内存大小
- jQuery的Cookie使用
- .net 下语音合成
- foxpro 打印 字体_【部编版同步复习】16年级上册期中预测卷,可打印!
- 64位程序怎么判断指针是否有效_AArch64应用程序级编程模型
- Java基础---学Java怎么能不了解多线程---Lambda表达式
- Bootstrap分页传值问题
- 内容页嵌套母版页时,内容页如何调用css和javascript
- libusb libusbk
- 在Linux上安装IDA的命令,Linux安装IDA神器
- 文件的创建、删除、移动和查找
- win10下安装appium
- <树莓派>——无法向U盘写入文件
- 如何将Excel中的图表导出为图片
- 苹果腕表应用上架 开发要求_苹果手表系列6首发
- 工作记录 io流写入linux文件
- 为什么你的蓝牙耳机总是出毛病,不好好反思一下这些问题吗?