安卓关于EditText的简单用法及intent传输ArrayList
EditText的简单用法
EditText,文本编辑框,在现实生活中使用很多很多,比如登入页面的账号密码输入框,打电话时输入电话号码,发邮件写的邮箱,而Android Studio又仔细对这些进行细分(很实用),比如一般的文字编辑框,使用Plain Text,一些可见数字用Number,一些不可见数字字符集合用Password,需求不同,显示方式也不同,放张图,就不一一说了。
实例
这里来一个实例,写一个学生的姓名,年龄,性别并显示出来,使用LinearLayout布局,三个编辑框都一样,直接复制粘贴改一下就行了,为了节省空间我这里就放一个,xml代码如下。
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="@string/name"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
values下的strings添加:
<string name="name">姓名</string>
ems属性表示的是编辑框的长度占几个字符。
inputType表示编辑框类型。
hint属性是在用户还没有输入的时候进行的半透明提示
这是XML的效果图:
活动
接下来写活动,要达到用户写完提交以后显示出来。
先将EditText、Button、textView作为私有的全局变量,方便后面的使用,用findViewById()通过id号找到对应的控件,都是一样的方法,写完一个直接copy成下一个
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private Button button3;
EditText editText1;
EditText editText2;
EditText editText3;
TextView textView1;
TextView textView2;
TextView textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button1);
button2=findViewById(R.id.button2);
editText1=findViewById(R.id.editText1);
editText2=findViewById(R.id.editText2);
editText3=findViewById(R.id.editText3);
textView1=findViewById(R.id.textView1);
textView2=findViewById(R.id.textView2);
textView3=findViewById(R.id.textView3);
button1.setOnClickListener(new View.OnClickListener() {
@Overrid