安卓编程 使EditText无法输入只能进行复制等操作

来源:互联网 发布:园林设计软件有哪些 编辑:程序博客网 时间:2024/05/29 12:23

背景:要实现像QQ等聊天工具那样,两个EditText,一个输入发送的内容,一个展示通讯内容,用于展示通讯内容的EditText不需要编辑,但是需要保留复制的功能。

方法:

借助隐藏输入法软键盘的方法。   ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);//隐藏软键盘    

在Edittext的单击事件中添加 隐藏软键盘 ,发现在输入框与展示框切换时无法隐藏软键盘,原因是切换时不触发单击事件而触发焦点改变事件,然后在焦点改变事件中添加隐藏软键盘的操作,但是仍然无法达到效果,因为软键盘显示操作在焦点改变事件之后,所以我们需要借助 计时器 延时执行隐藏软键盘的操作,取得了成功。

 

 

布局文件:  activity_main.xml

<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" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="Button" />    <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_above="@+id/editText2"        android:layout_alignParentTop="true"        android:layout_alignRight="@+id/button1"        android:ems="10" >        <requestFocus />    </EditText>    <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"        android:layout_alignLeft="@+id/editText1"        android:layout_toLeftOf="@+id/button1"        android:ems="10" /></RelativeLayout>

 

主文件:MainActivity.java

package com.example.edittext_unchange;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnFocusChangeListener;import android.view.View.OnClickListener;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class MainActivity extends Activity { private EditText showmsg = null; private Timer timer;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    showmsg = (EditText) findViewById(R.id.editText1);    showmsg.setOnClickListener(new ShowListener());//注册单击监听    showmsg.setOnFocusChangeListener(new OnFocusChangeListenerImpl());//注册焦点监听    }    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 OnFocusChangeListenerImpl implements OnFocusChangeListener {//设置焦点事件public void onFocusChange(View v, boolean hasFocus) { //具体要实现的部分// TODO Auto-generated method stub// 创建一个Timer定时器              timer = new Timer();             // 创建一个TimerTask              // TimerTask是个抽象类,实现了Runnable接口,所以TimerTask就是一个子线程              TimerTask timerTask = new TimerTask() {                 @Override                 public void run() {                     // 设置执行的任务        ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);//隐藏软键盘                 }             }; if (v.getId() == showmsg.getId()) {if (hasFocus) { //判断是否获得焦点     timer.schedule(timerTask,100);}}}}private class ShowListener implements OnClickListener {public void onClick(View arg0) {// TODO Auto-generated method stub((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);//隐藏软键盘 }}    }


再简单说一下计时器调用时的用法:

            // 定义计划任务,根据参数的不同可以完成以下种类的工作: 
            // 1.schedule(TimerTask task, Date when) ー> 在固定时间执行某任务 
            // 2.schedule(TimerTask task, Date when, long period) ー> 在固定时间开始重复执行某任务,重复时间间隔可控 
            // 3.schedule(TimerTask task, long delay) ー> 在延迟多久后执行某任务 
            // 4.schedule(TimerTask task, long delay, long period) ー> 在延迟多久后重复执行某任务,重复时间间隔可控  
         

 

 

0 0