예제 #1
0
 @Override
 protected void onRestart() {
   mAdapter.notifyDataSetChanged();
   // 关闭软键盘
   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
   // 得到InputMethodManager的实例
   if (imm.isActive()) {
     // 如果开启
     imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
     // 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
   }
   robotName = MyApplication.getString(HttpUtils.ROBOT_NIKE_NAME, "小黄鸭");
   title.setText(robotName);
   str = "您还记得大明湖畔的" + robotName + "吗?就是我喽," + "人称江湖百晓生,熟知段子、新闻、天气、列车、菜谱、航班、百度百科,很高兴为您服务!";
   mAdapter.changeFirstItem(str);
   super.onRestart();
 }
예제 #2
0
  private <T> void initView() {
    cmList = new ArrayList<ChatMessage>();
    title = (TextView) findViewById(R.id.title);
    robotName = MyApplication.getString(HttpUtils.ROBOT_NIKE_NAME, "小黄鸭");
    title.setText(robotName);
    new Thread(
            new Runnable() {

              @Override
              public void run() {
                List<ChatMessage> tempList = new ArrayList<ChatMessage>();
                tempList = dbUtiles.queryChatMessageList();
                if (tempList.size() == 0 || tempList == null) {
                  ChatMessage cm1 =
                      new ChatMessage(
                          1,
                          "您还记得大明湖畔的"
                              + robotName
                              + "吗?就是我喽,"
                              + "人称江湖百晓生,熟知段子、新闻、天气、列车、菜谱、航班、百度百科,很高兴为您服务!");
                  cmList.add(cm1);
                  cm1.setListType(0);
                  dbUtiles.insert(cm1);
                } else {
                  cmList.addAll(tempList);
                }
                handler.sendEmptyMessage(100);
              }
            })
        .start();

    lv = (ListView) findViewById(R.id.id_chat_listView);
    iv = (ImageView) findViewById(R.id.iv_add);
    iv.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            addPop.showAsDropDown(iv, iv.getLayoutParams().width / 2, 18);
          }
        });
    mAdapter = new ChatListAdatper(this, cmList, dbUtiles);
    lv.setAdapter(mAdapter);
    lv.setSelection(cmList.size() - 1);

    et = (EditText) findViewById(R.id.id_chat_msg);
    btn_send = (Button) findViewById(R.id.id_chat_send);
    btn_send.setOnClickListener(
        new OnClickListener() {

          @SuppressLint("ShowToast")
          @Override
          public void onClick(View v) {
            myMsg = et.getText().toString();
            if (TextUtils.isEmpty(myMsg)) {
              Toast.makeText(MainActivity.this, "请输入您要发送的内容", 0).show();
              return;
            } else {
              ChatMessage to = new ChatMessage(2, myMsg);
              to.setDate(new Date());
              to.setName(MyApplication.getString(HttpUtils.NIKE_NAME, "我"));
              to.setListType(0);
              cmList.add(to);
              dbUtiles.insert(to);
              mAdapter.notifyDataSetChanged();
              lv.setSelection(cmList.size() - 1);
              et.setText("");
            }
            // 关闭软键盘
            InputMethodManager imm =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            // 得到InputMethodManager的实例
            if (imm.isActive()) {
              // 如果开启
              imm.toggleSoftInput(
                  InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
              // 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的
            }
            if (!isNetworkAvailable()) {
              Toast.makeText(MainActivity.this, "请检查您的网络连接!", 0).show();
              return;
            }
            new Thread(
                    new Runnable() {

                      @Override
                      public void run() {
                        ChatMessage from = HttpUtils.sendMsg(myMsg);
                        from.setType(1);
                        from.setDate(new Date());
                        from.setName(robotName);

                        Message msg = handler.obtainMessage();
                        msg.obj = from;
                        msg.what = 1000;
                        handler.sendMessage(msg);
                      }
                    })
                .start();
          }
        });
  }