RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
编辑框EditText与按钮Button

在一个应用中,登录是经常使用的,下面我们学习一下如何开发一个登录窗口。我们需要学习Android中的基本控件:(1)EditText编辑框、(2)Button按钮。

创新互联服务项目包括崇明网站建设、崇明网站制作、崇明网页制作以及崇明网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,崇明网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到崇明省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

一、设计登录窗口

 打开“res/layout/activity_main.xml”文件。
   1、分别从工具栏向activity拖出2个EditText(来自Text Fields)、1个按钮(来自Form Widgets)。

2、打开activity_main.xml文件。
  代码自动生成如下:注意①和②虽同为EditText,但②要输入密码,故android:inputType="textPassword“。

3、我们把以上代码修改成如下代码,具体为:editText1变为userName;eidtText2变为passWord;buttion1变为login。登录按钮的文本:android:text="Button"变为"登录"。

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

   

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

   

        android:id="@+id/userName"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="52dp"

        android:ems="10" >

       

   

   

        android:id="@+id/passWord"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/userName"

        android:layout_below="@+id/userName"

        android:ems="10"

        android:inputType="textPassword" />

   

        android:id="@+id/login"

       

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/passWord"

        android:layout_marginTop="14dp"

        android:text="@string/login" />

现在运行程序,已经在手机上看起来很像一个登录窗口了。但是,我们单击“登录”按钮,却没有什么反应。我们下面学习如何在“登录”按钮上添加单击事件。

二、单击事件 

 打开“src/com.genwoxue.edittextbutton/MainActivity.java”文件。
  然后输入以下代码:

package com.example.hw;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

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 {

private EditText tvUserName = null;

private EditText tvPassWord = null;

private Button btnLogin = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tvUserName = (EditText) super.findViewById(R.id.userName);

tvPassWord = (EditText) super.findViewById(R.id.passWord);

btnLogin = (Button) super.findViewById(R.id.login);

btnLogin.setOnClickListener(new LoginOnClickListener());

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

private class LoginOnClickListener implements OnClickListener{

public void onClick(View v) {

// TODO 自动生成的方法存根

String username = tvUserName.getText().toString();

String password = tvPassWord.getText().toString();

String info = "用户名:"+username+"    密码:"+password;

Toast.makeText(getApplicationContext(), info,                          Toast.LENGTH_SHORT).show();

}

}

}

1、第①部分
 导入5个包。OnClickListener不要导错了。否则btnLogin.setOnClickListener(new LoginOnClickListener());报错

 2、第②部分
 声明三个控件变量。

 3、第③部分
 这一部分findViewById()方法是一个关键,这个方法表示从R.java文件中找到一个View(注意:我们可以把控件和Acitivity都当成一个View)。例如,tvUserName=(EditText)super.findViewById(R.id.userName)表示我们从R文件中找到userName代表的控件最后返给tvUserName,下一步我们可以通过tvUserName.getText()方法进一步获取到它的值。
 另一个关键是就是给“登录”按钮添加单击监听事件:btnLogin.setOnClickListener(new LoginOnClickListener())。

 4、第④部分
 我们新建一个类LoginOnClickListener继承接口OnClickListener用以实现单击事件监听。
  Toast.makeText(getApplicationContext(), info,Toast.LENGTH_SHORT).show()用以提示输入的用户名和密码。


分享名称:编辑框EditText与按钮Button
地址分享:http://scyingshan.cn/article/gipjdd.html