在聊天应用程序中编辑文本发送功能

时间:2018-06-19 08:15:21

标签: java android textwatcher

我正在创建一个叙事游戏,因为我有一个对话屏幕。

用户必须选择预定义的消息才能发送它。 选择的消息在编辑文本中传输,但要发送它,用户必须打开快速键盘并单击"发送"。

我想创建一个在用户选择后自动发送消息的方法。 (跳过手册"在键盘上发送"

我试过TextWatcher:

EditText userInput;
RecyclerView recyclerView;
List<ResponseMessage> responseMessageList;
MessageAdapter messageAdapter;
String[] listChoices;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chapitretest);

  Button btn = (Button) findViewById(R.id.btnChoice);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
              listChoices = new String[]{"Mdr","Lol","xd"};
            AlertDialog.Builder mBuilder= new AlertDialog.Builder(ChapTest.this);
            mBuilder.setTitle("Ton choix:");
            mBuilder.setSingleChoiceItems(listChoices, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    userInput.setText(listChoices[i]);
                    userInput.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                            String txtbulle = userInput.getText().toString();
                        }
                    });

                    dialogInterface.dismiss();
                }
            });
            AlertDialog mDialog = mBuilder.create();
            mDialog.show();

这是我的第一个应用程序,谢谢;)

更新:为什么我使用EditText :(会话方法)

  userInput = findViewById(R.id.UserInput);

     recyclerView=findViewById(R.id.Conversation);
     responseMessageList = new ArrayList<>();
     messageAdapter=new MessageAdapter(responseMessageList,this);
     recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
     recyclerView.setAdapter(messageAdapter);
    userInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {


            if(actionId == EditorInfo.IME_ACTION_SEND){
               ResponseMessage message = new ResponseMessage(userInput.getText().toString(),true);
               responseMessageList.add(message);
               ResponseMessage message2 = new ResponseMessage(userInput.getText().toString(),false);
                responseMessageList.add(message2);
                messageAdapter.notifyDataSetChanged();

                if (!isLastVisible())
                    recyclerView.smoothScrollToPosition(messageAdapter.getItemCount() - 1);
            }
            return false;
        }
    });
}

0 个答案:

没有答案