상세 컨텐츠

본문 제목

안드로이드 WebView, EditText 이용한 그나마 매끄로운 채팅창 구현

스마트기기개발관련/안드로이드 개발

by AlrepondTech 2011. 7. 21. 18:39

본문

반응형

 

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 

import...
import...
...

public class CChatView extends CBaseActivity implements OnTouchListener,OnClickListener,OnFocusChangeListener
{
    //html 한글 utf 8 기본 설정
    //base html
    //final String   HT_HEAD_S  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><html><head></head><body>";
    //final String   HT_HEAD_E  = "</body></html>";
   
    //android html
    final String HT_HEAD_S  = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <html><head><style type='text/css'><!--.talk_list {width:300; font-size:18px;color:#000000;font-family:AppleGothic;margin:0px;text-align:justify; line-height:1.6em;padding:10px 10px 10px 10px;} .talk_list a {font-size:14px;text-decoration:underline; color:#0000c8;}.talk_list span {padding:0 1px 0 1px;}.ico {vertical-align:text-bottom;}--></style></head><body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' bgcolor='#ebeef2'><div class='talk_list'>";
    final String HT_HEAD_SCREND = "<SCRIPT language=\"JavaScript\"> window.scroll(0, document.body.offsetHeight); </SCRIPT>";
    final String HT_HEAD_E  = "<br></div></body></html>";
   
    //web
    final String   HT_HREF_S  = "<a href=''>";
    final String   HT_HREF_E  = "</a>";
    public String _webdata  = "";
    final int     _max_lines = 50;
    ArrayList<String> _arrWebData = new ArrayList<String>();
   
    //control
    Intent m_parent           = null;
    CTitleBar   _titleBar     = null;
    WebView     _web          = null;
    ScrollView  _webScr       = null;
    EditText    _editChat     = null;
    ImageButton _cmtBtn       = null;
    boolean     _oneTouch     = false;
    InputMethodManager _immKeyboard  = null;
    int _chatTextLineHeight = 0;
    int _groom = 0;
   
    int _editstate = 0;
    boolean bEditFocus = false;
   
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.cchatview);
     
      _editChat  = (EditText)findViewById(R.id.chatedit);
      _cmtBtn    = (ImageButton)findViewById(R.id.cmtbtn);
      _cmtBtn.setOnClickListener(this);
      _web       = (WebView)findViewById(R.id.web);
      _webScr    = (ScrollView)findViewById(R.id.cchatview_scroll);
     
      _web.setWebViewClient(new MyWebClient());
      _web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
      _web.getSettings().setJavaScriptEnabled(true);
      _web.setOnClickListener(this);
      _web.setOnTouchListener(this);
      _web.setOnFocusChangeListener(this);
     
      _editChat.setOnFocusChangeListener(this);
      _editChat.setOnTouchListener(new OnTouchListener() {
       
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            _editstate = 1;
            return false;
        }
    });
     
      _editChat.setOnKeyListener(new View.OnKeyListener()
      {
        public boolean onKey(View v, int keyCode, KeyEvent event)
          {
              // TODO Auto-generated method stub
              if(keyCode == event.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP)
              {
                  LoadEdit();
              }
             
              if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
              {
                  finish();
              }
         
              return true;
          }
      });
     
     
          _immKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
           getSystemService(Context.INPUT_METHOD_SERVICE);
           _editChat.setImeOptions(EditorInfo.IME_ACTION_SEND);
          _web.requestFocus();
   
         
          open();
          HidekeyBoard();
    }
   
   
    void CheckKeyBoard()
    {
        if(_immKeyboard == null)
        {
            return;
        }
       
        if(bEditFocus && !_immKeyboard.isAcceptingText())
        {
            _editChat.setFocusableInTouchMode(true);
            _editChat.requestFocus();
            ShowkeyBoard();
        }
    }
   
 
    //----------------------------------------------------------------
    //
 
    public void ShowkeyBoard()
    {
        _immKeyboard.showSoftInput(_editChat, InputMethodManager.SHOW_FORCED);
    }
 
    public void HidekeyBoard()
    {
        bEditFocus = false;
        _immKeyboard.hideSoftInputFromWindow(_editChat.getWindowToken(), 0);
    }

 
    class MyWebClient extends WebViewClient
    {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        int act = event.getAction();
        if(event.getPointerCount() == 1)
        {
            switch(act)
            {
            case MotionEvent.ACTION_DOWN:
            {
                _oneTouch = true;
            }
            break;
            case MotionEvent.ACTION_MOVE:
            {
                _oneTouch = false;
            }
            break;
            case MotionEvent.ACTION_UP:
            {
                if(_oneTouch && _immKeyboard != null)
                {
                    HidekeyBoard();
                }
            }
            break;
            default:
            break;
            };   
        }
        return false;
    }

    public void LoadEdit()
    {
        String msg = _editChat.getText().toString();
        if(msg.length() > 0)
        {
            CNetwork.getInstance().sendChat(msg);
            _editChat.setText("");
        }
    }
 
   


    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if(_editstate == 1 && hasFocus)
        {
            _editstate = 0;
            bEditFocus = true;
            ShowkeyBoard();
        }
       
        CheckKeyBoard();
    }
   
    //------------------------------------------------------------------------
    //
    /**
     *
     */
   
   
    public void LoadHTML()
    {
        if(_arrWebData.size() == 0)
        {
            return;
        }
       
        if(_web == null)
        {
            return;
        }
       
        _webdata = "";
        _webdata = HT_HEAD_S;
       
        String temData = "";
        for(int i=0; i<_arrWebData.size(); i++)
        {
            temData = temData  + _arrWebData.get(i).toString();
        }
       
       
        _webdata = _webdata + temData;
        _webdata = _webdata + HT_HEAD_E;


        Handler handler = new Handler();
        handler .post(new Runnable()
        {
            public void run()
            {
                // TODO Auto-generated method stub
                _web.invalidate();
                _webScr.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }
       
    public void Clear()
    {
        if(_arrWebData.size() == 0)
        {
            return;
        }
       
        if(_web == null)
        {
            return;
        }
       
        _webdata = "";
        _webdata = _webdata + HT_HEAD_S;
        _webdata = _webdata + HT_HEAD_E;
        _web.loadData(_webdata, "text/html", "UTF-8");
    }

    public void SetComment(String comment)
    {
        if(comment.length() <= 4)
        {
            return;
        }

        _arrWebData.add(comment);
        if(_arrWebData.size() > _max_lines)
        {
            _arrWebData.remove(0);
        }
        LoadHTML();   
    }

    public void setMessage(String head, String body)
    {
        String msg;
        msg = "<font color='#0000C8'>"+head+"</font>"+body+"<br>";
        _arrWebData.add(msg);
       
        if(_arrWebData.size() > _max_lines)
        {
            _arrWebData.remove(0);
        }   
        LoadHTML();   
    }
   
}

 

 

=================================

=================================

=================================

 

 

반응형


관련글 더보기

댓글 영역