1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【Android开发】文本框 按钮 文本编辑框 提交登录 单选框

【Android开发】文本框 按钮 文本编辑框 提交登录 单选框

时间:2020-11-13 03:11:01

相关推荐

【Android开发】文本框 按钮 文本编辑框 提交登录 单选框

程序中用到的图标,可以到EasyIcon去下载,样式和大小都比较全

界面功能

目录结构

样式浏览

Main

java代码

package com.hanquan.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {//声明控件private Button mBtnTextView;private Button mBtnButton;private Button mBtnEditText;private Button mRadioButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//设置布局/* mBtnTextView = findViewById(R.id.btn_textview);mBtnTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到TextView演示页面Intent intent = new Intent(MainActivity.this, TextViewActivity.class);startActivity(intent);}});mBtnButton = findViewById(R.id.btn_button);mBtnButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到Button演示页面Intent intent = new Intent(MainActivity.this, ButtonActivity.class);startActivity(intent);}});mBtnEditText = findViewById(R.id.btn_edittext);mBtnEditText.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到btn_edittext演示页面Intent intent = new Intent(MainActivity.this, BtnEditTextActivity.class);startActivity(intent);}});mRadioButton = findViewById(R.id.btn_radiobutton);mRadioButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//跳转到btn_radiobutton演示界面Intent intent = new Intent(MainActivity.this, RadioButtonActivity.class);startActivity(intent);}});}*///找到控件mBtnTextView = findViewById(R.id.btn_textview);mBtnButton = findViewById(R.id.btn_button);mBtnEditText = findViewById(R.id.btn_edittext);mRadioButton = findViewById(R.id.btn_radiobutton);setListeners();}//设置所有控件的监听事件private void setListeners() {OnClick oc = new OnClick();mBtnTextView.setOnClickListener(oc);mBtnButton.setOnClickListener(oc);mBtnEditText.setOnClickListener(oc);mRadioButton.setOnClickListener(oc);}//设置所有控件的点击跳转位置class OnClick implements View.OnClickListener {@Overridepublic void onClick(View v) {Intent intent = null;//设置跳转对象switch (v.getId()) {case R.id.btn_textview:intent = new Intent(MainActivity.this, TextViewActivity.class);break;case R.id.btn_button:intent = new Intent(MainActivity.this, ButtonActivity.class);break;case R.id.btn_edittext:intent = new Intent(MainActivity.this, BtnEditTextActivity.class);break;case R.id.btn_radiobutton:intent = new Intent(MainActivity.this, RadioButtonActivity.class);break;}//执行跳转startActivity(intent);}}}

xml代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="15dp"><Buttonandroid:id="@+id/btn_textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="查看文本~" /><Buttonandroid:id="@+id/btn_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="各种 Button 效果"android:textAllCaps="false" /><Buttonandroid:id="@+id/btn_edittext"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="文本编辑框测试" /><Buttonandroid:id="@+id/btn_radiobutton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="选项框测试" /></LinearLayout>

界面1 查看文本

java代码

package com.hanquan.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.graphics.Paint;import android.os.Bundle;import android.text.Html;import android.widget.TextView;public class TextViewActivity extends AppCompatActivity {//声明空间private TextView mTv4;//中划线 消除锯齿private TextView mTv5;//中划线 不消除锯齿private TextView mTv6;//下划线 消除锯齿private TextView mTv7;//使用htmlprivate TextView mTv8;//跑马灯效果@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_text_view);mTv4 = findViewById(R.id.tv_4);//找到空间mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//文字中划线mTv4.getPaint().setAntiAlias(true);//消除锯齿mTv5 = findViewById(R.id.tv_5);mTv5.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//文字中划线mTv6 = findViewById(R.id.tv_6);mTv6.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//文字中划线mTv6.getPaint().setAntiAlias(true);//消除锯齿mTv7 = findViewById(R.id.tv_7);mTv7.setText(Html.fromHtml("<u>使用html中的下划线</u>"));mTv8 = findViewById(R.id.tv_8);mTv8.setSelected(true);}}

xml代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="20dp"><!-- 普通文字框--><TextViewandroid:id="@+id/tv_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="@string/tv_test1"android:textColor="#000000"android:textSize="20sp" /><!--超出则省略文字框--><!--ellipsize决定了当文字过长时,该控件该如何显示--><TextViewandroid:id="@+id/tv_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:ellipsize="end"android:maxLines="1"android:text="@string/tv_test1"android:textColor="#000000"android:textSize="20sp" /><!--添加图片--><TextViewandroid:id="@+id/tv_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:drawableRight="@drawable/triangle"android:drawablePadding="5dp"android:text="筛选"android:textColor="#3F51B5"android:textSize="20sp" /><!-- 文字中划线(消除锯齿)--><TextViewandroid:id="@+id/tv_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="这是一行文字,文字的中划线在java中产生,并且使用setAntiAlias(true);成功删除了锯齿"android:textColor="#000000"android:textSize="20sp" /><!-- 文字中划线(未消除锯齿)--><TextViewandroid:id="@+id/tv_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="这是一行文字,文字的中划线在java中产生,并且含有锯齿"android:textColor="#000000"android:textSize="20sp" /><!-- 文字下划线(消除锯齿)--><TextViewandroid:id="@+id/tv_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="文字下划线测试"android:textColor="#000000"android:textSize="20sp" /><!-- 使用html--><TextViewandroid:id="@+id/tv_7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text=""android:textColor="#000000"android:textSize="20sp" /><!--ellipsize="marquee"跑马灯的效果--><TextViewandroid:id="@+id/tv_8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:ellipsize="marquee"android:singleLine="true"android:text="我不想被人画个圈圈成为有些人喜闻乐道的对象;我不想因为弄些子虚乌有的“新闻”被有些人熟知;我只想专注于我的工作、我的职业;我只想真诚的面对我的生活;我只想单纯的热爱我爱的一切。这条路我不急,急什么呢?争抢?欺骗?我做不到也学不会。也不希望我身边的人玩那些大家都懂的手段。也许让有的人失望了,没能成为你们想要看到的那一种星星。只想说我选择了我喜欢的职业,前进的每一步都希望坚实。懂的不必解释;不懂的,无须表达。我就是我,我还是我,我永远是我。………今天表达了很多,其实一直在心里最想感谢的是无私付出的你们~希望我们可以一直默契的,默契的走下去!!"android:textColor="#000000"android:textSize="20dp"android:marqueeRepeatLimit="marquee_forever"android:focusable="true"android:focusableInTouchMode="true"/></LinearLayout>

界面2 各种button效果

java代码

package com.hanquan.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class ButtonActivity extends AppCompatActivity {private Button mBtn3;private TextView mTv1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button);mBtn3 = findViewById(R.id.btn_3);mBtn3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(ButtonActivity.this, "button3被点击一次", Toast.LENGTH_SHORT).show();}});mTv1 = findViewById(R.id.tv_1);mTv1.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){Toast.makeText(ButtonActivity.this, "text文本被点击一次", Toast.LENGTH_SHORT).show();}});}public void showToast(View view) {Toast.makeText(this, "让你点了吗", Toast.LENGTH_SHORT).show();}}

xml代码

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/btn_1"android:layout_width="match_parent"android:layout_height="80dp"android:layout_margin="20dp"android:background="#FFC107"android:text="点我点我"android:textColor="#CA517B"android:textSize="30sp" /><!--在drawble中创建了一个背景样式--><Buttonandroid:id="@+id/btn_2"android:layout_width="match_parent"android:layout_height="80dp"android:layout_below="@id/btn_1"android:layout_margin="20dp"android:background="@drawable/bg_btn2"android:text="点我哈哈点我"android:textColor="#000000"android:textSize="30sp" /><!--描边--><Buttonandroid:id="@+id/btn_3"android:layout_width="match_parent"android:layout_height="80dp"android:layout_below="@id/btn_2"android:layout_margin="20dp"android:background="@drawable/bg_btn3"android:text="第三个按钮"android:textColor="#E91E63"android:textSize="30sp" /><!--按下反馈--><Buttonandroid:id="@+id/btn_4"android:layout_width="match_parent"android:layout_height="80dp"android:layout_below="@id/btn_3"android:layout_margin="20dp"android:background="@drawable/bg_btn4"android:onClick="showToast"android:text="第四个按钮"android:textColor="#E91E63"android:textSize="30sp" /><TextViewandroid:id="@+id/tv_1"android:layout_width="match_parent"android:layout_height="50dp"android:textColor="#000000"android:text="这是一行文字"android:layout_below="@id/btn_4"android:layout_margin="10pt"android:textSize="25sp"android:background="#F44336"android:gravity="center"/></RelativeLayout>

界面3 文本编辑框测试

java代码

package com.hanquan.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class BtnEditTextActivity extends AppCompatActivity {private Button mBtnLogin;private EditText mUid;private EditText mUpwd;private static String strID="";private static String strPWD="";@Overrideprotected void onCreate(final Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_btn_edit_text);mBtnLogin = findViewById(R.id.btn_login);mBtnLogin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Log.d("用户名", strID);Log.d("密码", strPWD);if (strID.equals("魔鬼鱼") && strPWD.equals("123456")) {Toast.makeText(BtnEditTextActivity.this, "登录成功!" + strID + " " + strPWD, Toast.LENGTH_SHORT).show();} else {Toast.makeText(BtnEditTextActivity.this, "登录失败!" + strID + " " + strPWD, Toast.LENGTH_SHORT).show();}}});mUid = findViewById(R.id.et_1);mUid.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {Log.d("正在输入用户名", charSequence.toString());strID = charSequence.toString();}@Overridepublic void afterTextChanged(Editable editable) {}});mUpwd = findViewById(R.id.et_2);mUpwd.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {Log.d("正在输入密码", charSequence.toString());strPWD = charSequence.toString();}@Overridepublic void afterTextChanged(Editable editable) {}});}}

xml代码

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"><TextViewandroid:id="@+id/tv_welcome"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="欢迎你~ 请登录"android:textSize="30dp" /><EditTextandroid:id="@+id/et_1"android:layout_width="match_parent"android:layout_height="50dp"android:layout_below="@id/tv_welcome"android:layout_marginTop="50dp"android:background="@drawable/bg_username"android:drawableLeft="@drawable/pic_usr"android:drawablePadding="5dp"android:hint="用户名"android:padding="10dp" /><EditTextandroid:id="@+id/et_2"android:layout_width="match_parent"android:layout_height="50dp"android:layout_below="@id/et_1"android:layout_marginTop="20dp"android:background="@drawable/bg_username"android:drawableLeft="@drawable/pic_pwd"android:drawablePadding="5dp"android:hint="密码"android:inputType="textPassword"android:padding="10dp" /><Buttonandroid:id="@+id/btn_login"android:layout_width="match_parent"android:layout_height="50dp"android:layout_below="@id/et_2"android:layout_marginTop="50dp"android:text="登录"android:textSize="20sp"android:background="@drawable/bg_btn4"android:textColor="#FFFFFF"/>/></RelativeLayout>

界面4 选择框测试

java代码

package com.hanquan.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioButtonActivity extends AppCompatActivity {private RadioGroup mRg1;private RadioGroup mRg2;private Button mBtn1;private Button mBtn2;int id1 = 0;int id2 = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio_button);mRg1 = findViewById(R.id.rg_1);mRg2 = findViewById(R.id.rg_2);//第一种选择方式mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {//这里的i是一个随机分配的大数RadioButton radioButton = radioGroup.findViewById(i);Toast.makeText(RadioButtonActivity.this, "你的选择是 " + radioButton.getText(), Toast.LENGTH_SHORT).show();if (radioButton.getText().equals("男")) {id1 = 0;} else if (radioButton.getText().equals("女")) {id1 = 1;} else {id1 = 2;}}});//第二种选择方式mRg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup radioGroup, int i) {//这里的i是一个随机分配的大数RadioButton radioButton = radioGroup.findViewById(i);Toast.makeText(RadioButtonActivity.this, "你的选择是 " + radioButton.getText(), Toast.LENGTH_SHORT).show();if (radioButton.getText().equals("男")) {id2 = 0;} else if (radioButton.getText().equals("女")) {id2 = 1;} else {id2 = 2;}}});//提交按钮1的点击mBtn1 = findViewById(R.id.btn_submit1);mBtn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {switch (id1) {case 0:Toast.makeText(RadioButtonActivity.this, "按钮1~男!", Toast.LENGTH_SHORT).show();break;case 1:Toast.makeText(RadioButtonActivity.this, "按钮1~女!", Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(RadioButtonActivity.this, "按钮1~未知性别!", Toast.LENGTH_SHORT).show();break;}}});//提交按钮2的点击mBtn2 = findViewById(R.id.btn_submit2);mBtn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {switch (id2) {case 0:Toast.makeText(RadioButtonActivity.this, "按钮2~男~", Toast.LENGTH_SHORT).show();break;case 1:Toast.makeText(RadioButtonActivity.this, "按钮2~女~", Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(RadioButtonActivity.this, "按钮2~未知性别~", Toast.LENGTH_SHORT).show();break;}}});}}

xml代码

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20sp"><TextViewandroid:id="@+id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="请选择你的性别"android:textSize="30sp" /><!--把RadioButton放在RadioGroup组中,形成单选框,只能单选--><!--checked定义是否默认选中,但一定要设置id才有效--><RadioGroupandroid:id="@+id/rg_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/text1"android:orientation="vertical"><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:checked="true"android:text="男"android:textSize="24sp" /><RadioButtonandroid:id="@+id/rb_girl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="女"android:textSize="24sp" /><RadioButtonandroid:id="@+id/rb_no"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="不知道"android:textSize="24sp" /></RadioGroup><Buttonandroid:id="@+id/btn_submit1"android:layout_width="match_parent"android:layout_height="40dp"android:layout_below="@id/rg_1"android:layout_marginTop="30dp"android:background="@drawable/bg_submit"android:text="提交"android:textColor="#ffffff"android:textSize="24dp" /><TextViewandroid:id="@+id/text2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/btn_submit1"android:gravity="center"android:paddingTop="50dp"android:text="想换一种方式选择?"android:textSize="30sp" /><RadioGroupandroid:id="@+id/rg_2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/text2"android:layout_marginTop="10dp"android:orientation="horizontal"android:paddingTop="20dp"><RadioButtonandroid:id="@+id/rb_boy2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_margin="12dp"android:layout_marginTop="20dp"android:layout_weight="1"android:background="@drawable/bg_radio1"android:button="@null"android:checked="true"android:gravity="center"android:text="男"android:textSize="24sp" /><RadioButtonandroid:id="@+id/rb_girl2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_margin="12dp"android:layout_marginTop="20dp"android:layout_weight="1"android:background="@drawable/bg_radio1"android:button="@null"android:gravity="center"android:text="女"android:textSize="24sp" /><RadioButtonandroid:id="@+id/rb_no2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_margin="12dp"android:layout_marginTop="20dp"android:layout_weight="1"android:background="@drawable/bg_radio1"android:button="@null"android:gravity="center"android:text="不知道"android:textSize="24sp" /></RadioGroup><Buttonandroid:id="@+id/btn_submit2"android:layout_width="match_parent"android:layout_height="40dp"android:layout_below="@id/rg_2"android:layout_marginTop="30dp"android:background="@drawable/bg_submit"android:text="提交"android:textColor="#ffffff"android:textSize="24dp" /></RelativeLayout>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。