溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

編輯框EditText與按鈕Button

發(fā)布時間:2020-06-25 20:35:28 來源:網(wǎng)絡 閱讀:774 作者:沒有水勒魚 欄目:移動開發(fā)

在一個應用中,登錄是經(jīng)常使用的,下面我們學習一下如何開發(fā)一個登錄窗口。我們需要學習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變?yōu)?/span>userName;eidtText2變?yōu)?/span>passWord;buttion1變?yōu)?/span>login。登錄按鈕的文本:android:text="Button"變?yōu)?/span>"登錄"。

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

 現(xiàn)在運行程序,已經(jīng)在手機上看起來很像一個登錄窗口了。但是,我們單擊登錄按鈕,卻沒有什么反應。我們下面學習如何在登錄按鈕上添加單擊事件。

二、單擊事件 

  打開“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()方法進一步獲取到它的值。
  另一個關鍵是就是給登錄按鈕添加單擊監(jiān)聽事件:btnLogin.setOnClickListener(new LoginOnClickListener())。

  4、第部分
 
 我們新建一個類LoginOnClickListener繼承接口OnClickListener用以實現(xiàn)單擊事件監(jiān)聽。
  Toast.makeText(getApplicationContext(), info,Toast.LENGTH_SHORT).show()用以提示輸入的用戶名和密碼。


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI