Android常见控件— — —EditText
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android常见控件— — —EditText相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输姓名"
android:maxLines="1"/>
<EditText
android:id="@+id/edit_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="学号"
android:maxLines="1"/>
</LinearLayout>
应用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输姓名"
android:maxLines="1"/>
<EditText
android:id="@+id/edit_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="请输入学号"
android:maxLines="1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认"
android:layout_gravity="center"
/>
</LinearLayout>
package com.example.uiwidgettest2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class edittextActivity extends Activity implements View.OnClickListener{
private EditText et1;
private EditText et2;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edittext);
btn = (Button)findViewById(R.id.button1);
et1 = (EditText)findViewById(R.id.edit_test1);
et2 = (EditText)findViewById(R.id.edit_test2);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
String username = et1.getText().toString();
String id = et2.getText().toString();
Toast.makeText(edittextActivity.this,username+id,Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
以上是关于Android常见控件— — —EditText的主要内容,如果未能解决你的问题,请参考以下文章