日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android监听输入框光标,EditText光标的移动

發(fā)布時(shí)間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android监听输入框光标,EditText光标的移动 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在做項(xiàng)目時(shí),我們可能會(huì)遇到當(dāng)輸入框的內(nèi)容變化后讓光標(biāo)自動(dòng)顯示在下一個(gè)輸入框,這樣就省去了手工的點(diǎn)擊,從而提高了效率。

requestFocus() 獲取焦點(diǎn) ?即光標(biāo)的顯示

setOnFocusChangeListener ?監(jiān)聽EditText焦點(diǎn)變化 ? 當(dāng)獲取焦點(diǎn)后hasFocus 為true

具體演示的代碼:

xml布局:

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/et_name"

android:layout_width="match_parent"

android:layout_height="40dp"

android:hint="請輸入姓名:" />

android:id="@+id/et_pass"

android:layout_width="match_parent"

android:layout_height="40dp"

android:hint="請輸入密碼:" />

android:id="@+id/btn_request"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="密碼框獲取焦點(diǎn)"

/>

android:id="@+id/btn_lose"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="密碼框失去焦點(diǎn)"

/>

java代碼:

package com.example.android_edittext;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnFocusChangeListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText mEtName,mEtPass;

private Button btnRequest,btnLose;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initEvents();

}

private void initEvents() {

btnRequest.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

mEtName.setText("zm");

mEtPass.requestFocus();

}

});

btnLose.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

mEtPass.setText("LP");

mEtName.requestFocus();

}

});

mEtPass.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if(hasFocus){//獲取焦點(diǎn)

Toast.makeText(MainActivity.this, "密碼框獲取到焦點(diǎn)了", Toast.LENGTH_SHORT).show();

}else{//失去焦點(diǎn)

Toast.makeText(MainActivity.this, "密碼框失去焦點(diǎn)了", Toast.LENGTH_SHORT).show();

}

}

});

}

private void initView() {

mEtName=(EditText) findViewById(R.id.et_name);

mEtPass=(EditText) findViewById(R.id.et_pass);

btnRequest=(Button) findViewById(R.id.btn_request);

btnLose=(Button) findViewById(R.id.btn_lose);

}

}

總結(jié)

以上是生活随笔為你收集整理的android监听输入框光标,EditText光标的移动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。