如何在片段中使用Emojicon

时间:2017-01-12 08:48:53

标签: android fragment

我用在片段中。

  

错误java.lang.IllegalArgumentException:MainActivity @ a3d41ec必须   实现接口OnEmojiconBackspaceClickedListener

public class Chat extends Fragment implements EmojiconGridFragment.OnEmojiconClickedListener,
        EmojiconsFragment.OnEmojiconBackspaceClickedListener {


EmojiconEditText mEditEmojicon;
EmojiconTextView mTxtEmojicon;
public Chat() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_chat, container, false);

    mEditEmojicon = (EmojiconEditText) view.findViewById(R.id.editEmojicon);
    mTxtEmojicon = (EmojiconTextView) view.findViewById(R.id.txtEmojicon);

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mEditEmojicon.addTextChangedListener(new TextWatcher() {

        /**
         * This notify that, within s,
         * the count characters beginning at start are about to be replaced by new text with length
         * @param s
         * @param start
         * @param count
         * @param after
         */
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        /**
         * This notify that, somewhere within s, the text has been changed.
         * @param s
         */
        @Override
        public void afterTextChanged(Editable s) {}

        /**
         * This notify that, within s, the count characters beginning at start have just
         * replaced old text that had length
         * @param s
         * @param start
         * @param before
         * @param count
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Set text on TextView
            mTxtEmojicon.setText(s);
        }
    });
    setEmojiconFragment(false);
}
private void setEmojiconFragment(boolean useSystemDefault) {

    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.emojicons, EmojiconsFragment.newInstance(useSystemDefault))
            .commit();
}
@Override
public void onEmojiconClicked(Emojicon emojicon) {
    EmojiconsFragment.input(mEditEmojicon, emojicon);
}

@Override
public void onEmojiconBackspaceClicked(View v) {
    EmojiconsFragment.backspace(mEditEmojicon);
}
}

2 个答案:

答案 0 :(得分:1)

主机活动必须实现处理后退按钮的界面 点击已完成的片段中的

https://github.com/rockerhieu/emojicon/issues/21

示例活动

package com.rockerhieu.emojicon.example;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import io.github.rockerhieu.emojicon.EmojiconEditText;
import io.github.rockerhieu.emojicon.EmojiconGridFragment;
import io.github.rockerhieu.emojicon.EmojiconTextView;
import io.github.rockerhieu.emojicon.EmojiconsFragment;
import io.github.rockerhieu.emojicon.emoji.Emojicon;

public class MainActivity extends AppCompatActivity implements EmojiconGridFragment.OnEmojiconClickedListener, EmojiconsFragment.OnEmojiconBackspaceClickedListener {
    EmojiconEditText mEditEmojicon;
    EmojiconTextView mTxtEmojicon;
    CheckBox mCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditEmojicon = (EmojiconEditText) findViewById(R.id.editEmojicon);
        mTxtEmojicon = (EmojiconTextView) findViewById(R.id.txtEmojicon);
        mEditEmojicon.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTxtEmojicon.setText(s);
            }
        });
        mCheckBox = (CheckBox) findViewById(R.id.use_system_default);
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mEditEmojicon.setUseSystemDefault(b);
                mTxtEmojicon.setUseSystemDefault(b);
                setEmojiconFragment(b);
            }
        });

        setEmojiconFragment(false);
    }

    private void setEmojiconFragment(boolean useSystemDefault) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.emojicons, EmojiconsFragment.newInstance(useSystemDefault))
                .commit();
    }

    @Override
    public void onEmojiconClicked(Emojicon emojicon) {
        EmojiconsFragment.input(mEditEmojicon, emojicon);
    }

    @Override
    public void onEmojiconBackspaceClicked(View v) {
        EmojiconsFragment.backspace(mEditEmojicon);
    }

    public void openEmojiconsActivity(View view) {
        startActivity(new Intent(this, EmojiconsActivity.class));
    }
}

如果您有疑问,请回过头来查看原始示例并进行比较 https://github.com/rockerhieu/emojicon/tree/master/example/src/main/java/com/rockerhieu/emojicon/example

答案 1 :(得分:0)

您需要为Activity

实现相同的侦听器
public class MainActivity extends AppCompatActivity implements EmojiconsFragment.OnEmojiconBackspaceClickedListener {

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


}