Android editText

仅供自己练习代码,对看博文的朋友,可能么啥帮助。 

1. res/layout/layout_edittext.xml 添加android布局文件(亦可以在main.xml 中,这里我新添加配置文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText 
        android:id="@+id/editTextOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

2.activity 代码

package com.tutoial.edittext;

import com.tutoial.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class EditTextTutorial extends Activity {

	private EditText editText = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_edittext);
		
		addKeyListener();
	}
	
	public void addKeyListener(){
		
		editText = (EditText)findViewById(R.id.editTextOne);
		//android 的事件 驱动和 swing 如出一撤
		editText.setOnKeyListener(new OnKeyListener(){
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				
				//按键输入信心 并 按下回车键
				if(event.getAction() == KeyEvent.ACTION_DOWN && 
											keyCode == KeyEvent.KEYCODE_ENTER){
					Toast.makeText(EditTextTutorial.this, editText.getText(), Toast.LENGTH_LONG).show();
					return true;
				}else if(event.getAction() == KeyEvent.ACTION_DOWN && 
											keyCode == KeyEvent.KEYCODE_9){ //按了九号键
					Toast.makeText(EditTextTutorial.this,"您老按下的9号键",Toast.LENGTH_SHORT).show();
					return true;
				}
				
				return false;
			
			}
			
		});
	}
}

怎么说呢。还是很容易理解的


Android editText_第1张图片

Android editText_第2张图片

你可能感兴趣的:(java,android)