温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

编辑框EditText与按钮Button

发布时间:2020-06-25 20:35:28 来源:网络 阅读:767 作者:没有水勒鱼 栏目:移动开发

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

一、设计登录窗口

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

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

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    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" >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

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


    <EditText

        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" >


        <requestFocus />

    </EditText>


    <EditText

        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" />


    <Button

        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" />


</RelativeLayout>

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

二、单击事件 

  打开“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()用以提示输入的用户名和密码。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI