<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" > <TableRow> <TextView android:gravity="right" android:textStyle="bold" android:padding="3dip" android:text="用户名称:" /> <EditText android:id="@+id/username" android:padding="3dip" android:scrollHorizontally="true" /> </TableRow> <TableRow> <TextView android:gravity="right" android:textStyle="bold" android:padding="3dip" android:text="用户密码:" /> <EditText android:id="@+id/password" android:padding="3dip" android:password="true" /> </TableRow> <TableRow android:gravity="right"> <Button android:id="@+id/cancel" android:text="取消" /> <Button android:id="@+id/login" android:text="登录" /> </TableRow> </TableLayout>
package com.rainsong.edittextdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { Button btn_login; OnClickListener listener_login; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listener_login = new OnClickListener() { public void onClick(View v) { EditText username = (EditText) findViewById(R.id.username); EditText password = (EditText) findViewById(R.id.password); Toast.makeText(MainActivity.this, "用户名称:" + username.getText() + ", 用户密码:" + password.getText(),Toast.LENGTH_SHORT).show(); } }; btn_login = (Button)findViewById(R.id.login); btn_login.setOnClickListener(listener_login); } }
原文地址:http://blog.csdn.net/hantangsongming/article/details/40581459