温馨提示:这篇文章已超过459天没有更新,请注意相关的内容是否还可用!
摘要:,,本教程介绍了Android Studio入门实战中的用户注册功能。通过简洁明了的步骤,指导读者完成从设计注册界面到实现注册功能的整个过程。本教程注重实战操作,适合初学者快速掌握Android Studio开发技巧,实现用户注册功能。让读者轻松掌握Android开发的基础知识,为后续的进阶学习打下基础。
完整代码在文末
目录
完整代码在文末
功能:
新建项目:
1.gradle 使用国内镜像源 加速
2.新建主类文件、布局文件:
1.如果已有主类文件但没有layout文件
2.MainActivity主类文件不是java语言,想要新建activity
3.布局文件代码:
1.所有的控件都是约束布局,顾名思义就是受限于parent的位置,一般一个控件的约束布局只需要两个方向的约束就可以拉(你不设置,它会flying) 如图:
3.Sppiner控件
编辑
完整代码:
第二个项目 选课中心已完成:http://t.csdnimg.cn/QQV1t
bug: 点击爱好和取消爱好 没有实时同步
点击性别隐藏,取消后,性别需要再选取
问题已解决,更新在文末完整代码中
功能:
1. 点击radiobutton 男女,赋值提示信息 帅哥/美女
2.点击switch 组件,隐藏radiobutton 按钮,性别提示信息
3.点击checkbox组件,赋值你的兴趣爱好信息
4.点击spinner组件,赋值城市信息
5.点击提交,弹出你的个人信息 如下图
效果图:
新建项目:
1.gradle 使用国内镜像源 加速
点击setting文件
添加如下代码,改为下图效果(速度噌噌上)
maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' }
2.新建主类文件、布局文件:
1.如果已有主类文件但没有layout文件
点击res文件右键 新建layout directionary目录 再新建布局文件xml
在你的主类里添加布局文件,如图所示:
2.MainActivity主类文件不是java语言,想要新建activity
点击第一个com.example,右键新建empty activity 选择语言java ,其中的layout文件也会初始化完成。
当然后续要设置该项目为启动项目(另外如果模拟器闪退了,兄弟! 模拟器没问题,去看看你滴locat的报错吧)
3.布局文件代码:
接下来我来讲讲该讲的重要控件代码:
1.所有的控件都是约束布局,顾名思义就是受限于parent的位置,一般一个控件的约束布局只需要两个方向的约束就可以拉(你不设置,它会flying) 如图:
2.Switch 控件(点击隐藏性别button)
//选中性别,绑定性别信息 boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Sex = "帅哥"; } } }); girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Sex = "美女"; } } }); //隐藏性别switch控件,并且隐藏信息text swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { radiogp.setVisibility(View.INVISIBLE); }else{ radiogp.setVisibility(View.VISIBLE); } } } );
3.Sppiner控件
首先决定好约束后,设置下拉列表中的item咯,在res 文件下,点击Strings.xml,在文件中添加如下代码。
再回到布局文件设置entries为string-array 的Id.
Spinner代码文件(选择item城市,赋值城市):
//City模块 spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { text_City += getResources().getStringArray(R.array.city)[position]; } @Override public void onNothingSelected(AdapterView adapterView) { } });
完整代码:
MainAcitivity2.java:
package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.Switch; import android.widget.Toast; public class MainActivity2 extends Activity { private Button subbtn; private EditText name; private EditText password; private Switch swi; private RadioGroup radiogp; private RadioButton boy; private RadioButton girl; public String text_Sex; private CheckBox basbtn; private CheckBox footbtn; private CheckBox badbtn; private CheckBox readbtn; private String text_Aihao; private Spinner spin; private String text_City; private EditText name_; private EditText password_; private String text_last; boolean b =true; String a = " "; @Override protected void onCreate(Bundle savedInstanceState) { text_Sex=""; text_Aihao=""; text_City=""; text_last=""; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); swi=(Switch) findViewById(R.id.switch1); radiogp=(RadioGroup)findViewById((R.id.radioGroup)); subbtn = (Button)findViewById(R.id.submitbtn); boy=(RadioButton)findViewById(R.id.boybutton); girl=(RadioButton)findViewById(R.id.girlbutton); spin=(Spinner)findViewById(R.id.spinner); name_=(EditText) findViewById(R.id.editTextText); password_=(EditText) findViewById(R.id.editTextText2); //选中性别,绑定性别信息 boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Sex = "帅哥"; } } }); girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Sex = "美女"; } } }); //隐藏性别switch控件,并且隐藏信息text swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { radiogp.setVisibility(View.INVISIBLE); }else{ radiogp.setVisibility(View.VISIBLE); } } } ); //兴趣爱好模块 basbtn = (CheckBox) findViewById(R.id.baseketballbtn); footbtn = (CheckBox) findViewById(R.id.footballbtn); badbtn = (CheckBox) findViewById(R.id.badminballbtn); readbtn = (CheckBox) findViewById(R.id.readbtn); basbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Aihao +="篮球"; } else{ text_Aihao = text_Aihao.replace("篮球",""); } } }); footbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Aihao +="足球"; } else{ text_Aihao = text_Aihao.replace("足球",""); } } }); badbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Aihao +="羽毛球"; } else{ text_Aihao = text_Aihao.replace("羽毛球",""); } } }); readbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ text_Aihao +="阅读"; } else{ text_Aihao = text_Aihao.replace("阅读",""); } } }); //City模块 spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { text_City = getResources().getStringArray(R.array.city)[position]; } @Override public void onNothingSelected(AdapterView adapterView) { } }); //submit 按钮 subbtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if(swi.isChecked()){ text_last=""; }else{ text_last=text_Sex; } Toast.makeText(MainActivity2.this,name_.getText()+text_last+"你好!你所在的城市是"+text_City+"!你的兴趣爱好是"+text_Aihao,Toast.LENGTH_SHORT).show(); } }); } }
activity_main.xml:
还没有评论,来说两句吧...