FoucusProject 焦点事件

来源:互联网 发布:新品牌网络推广 编辑:程序博客网 时间:2024/05/16 09:17


Mydemo.java

package com.jackie.foucusproject;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
    
private EditText edit;
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.edit);
txt=(TextView)findViewById(R.id.txt);
edit.setOnClickListener(new OnClickListenerImpl());
edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl());
}
class OnClickListenerImpl implements OnClickListener{


@Override
public void onClick(View arg0) {
edit.setText("");

}

}
class OnFocusChangeListenerImpl implements OnFocusChangeListener{


@Override
public void onFocusChange(View arg0, boolean hasFocus) {
if(hasFocus){
txt.setText("该组件获得焦点");
}else{
if(edit.getText().length()>0){
txt.setText("该组件失去焦点,且内容合法");
}else{
txt.setText("该组件失去焦点,且输入内容不能为空");
}

}

}}


@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;
}

}

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <EditText 
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请输入查询的内容"/>
     <EditText 
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="www.ihou.com"/>
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />


</LinearLayout>

0 0
原创粉丝点击