【问题标题】:clickable String in TextView into EditText insinde Adapter.classAdapter.class 中的 TextView 和 EditText 中的可点击字符串
【发布时间】:2020-08-21 12:46:40
【问题描述】:

我正在尝试设置与 IBM Watson Assistant 通信的聊天。助手有一个名为Options 的函数。我在我的ChatApdater 中实现了这些对象的功能,现在我想让我的选项可点击,所以每当我点击一个选项时,它就会显示在我的 EditText 字段中,您通常会在这里写下您的问题。示例with 4 options

我的ChatApdater 代码如下所示:

protected Activity activity;
private int SELF = 100;
private ArrayList<Message> messageArrayList;


public ChatAdapter(ArrayList<Message> messageArrayList) {
    this.messageArrayList = messageArrayList;

}



@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;

    // view type is to identify where to render the chat message
    // left or right
    if (viewType == SELF) {
        // self message
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chat_item_self, parent, false);
    } else {
        // WatBot message
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chat_item_watson, parent, false);
    }


    return new ViewHolder(itemView);
}

@Override
public int getItemViewType(int position) {
    Message message = messageArrayList.get(position);
    if (message.getId() != null && message.getId().equals("1")) {
        return SELF;
    }

    return position;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Message message = messageArrayList.get(position);
    switch (message.type) {
        case TEXT:
            ((ViewHolder) holder).message.setText(Html.fromHtml(message.getMessage()+"<br/>"));
                break;
        case IMAGE:
            ((ViewHolder) holder).message.setVisibility(View.GONE);
            ImageView iv = ((ViewHolder) holder).image;
            Glide
                    .with(iv.getContext())
                    .load(message.getUrl())
                    .into(iv);
            break;
        case OPTION:
            TextView tv = ((ViewHolder) holder).message;
            tv.setVisibility(View.GONE);
            LinearLayout optionsContainer = ((ViewHolder) holder).optionsContainer;
            TextView messageTextView = createStartView(message.getMessage(), optionsContainer.getContext());
            optionsContainer.addView(messageTextView);

            for ( DialogNodeOutputOptionsElement r : message.getOptions() ) {
                System.out.println("blblba");
                String option = r.getLabel();
                TextView optionTextView = createTextView(option, optionsContainer.getContext());
                // add the created textView to our container
                optionsContainer.addView(optionTextView);
            }
            break;
        case PAUSE:break;
    }



}
private TextView createTextView(final String text, final Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams
            ((int) LinearLayout.LayoutParams.WRAP_CONTENT,(int) LinearLayout.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(params);
    tv.setTextSize((float) 15);
    tv.setText(Html.fromHtml(text));
    int blueColor = Color.parseColor("#0000ff");
    // make text blue
    tv.setTextColor(blueColor);
    // make text underline
    tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          //  Toast.makeText(context, "Link clicked", Toast.LENGTH_SHORT).show();
            // add here what the click should do
        }

    });
    return tv;

}
private TextView createStartView(String text, Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams
            ((int) LinearLayout.LayoutParams.WRAP_CONTENT,(int) LinearLayout.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(params);
    tv.setTextSize((float) 15);
    tv.setText(Html.fromHtml(text));
    int blueColor = Color.parseColor("#0000ff");
    // make text blue

    // make text underline
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //  Toast.makeText(context, "Link clicked", Toast.LENGTH_SHORT).show();
            // add here what the click should do
        }

    });
    return tv;

}

@Override
public int getItemCount() {
    return messageArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView message;
    ImageView image;
    LinearLayout optionsContainer;
    EditText eingabe;

    public ViewHolder(View view) {
        super(view);
        message = (TextView) itemView.findViewById(R.id.message);
        image = (ImageView) itemView.findViewById(R.id.image);
        optionsContainer = (LinearLayout) itemView.findViewById(R.id.optionsContainer);
        eingabe = (EditText) itemView.findViewById(R.id.einmessage);








        //TODO: Uncomment this if you want to use a custom Font
        
    }
}
}

虽然重要的部分就在这里:

 private TextView createTextView(final String text, final Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams
            ((int) LinearLayout.LayoutParams.WRAP_CONTENT,(int) LinearLayout.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(params);
    tv.setTextSize((float) 15);
    tv.setText(Html.fromHtml(text));
    int blueColor = Color.parseColor("#0000ff");
    // make text blue
    tv.setTextColor(blueColor);
    // make text underline
    tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // STORE CLICKED OPTION IN EDITTEXT
        }

    });
    return tv;

}

我必须为我的聊天制作 3 个不同的 XML。一个用于 watson、myinput 和总共聊天(存储 Edittext 的位置)。 ChatApdater 中尚未提及此 XML。

XML 如下所示:

    <EditText
        android:id="@+id/einmessage"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="10dp"
        android:layout_weight="3"
        android:background="@null"
        android:hint="@string/what_s_on_your_mind"
        android:lines="1"
        android:paddingLeft="10dp" />

【问题讨论】:

    标签: java android watson-assistant


    【解决方案1】:

    TextView获取点击的项目:

    tv.setOnClickListener(new OnClickListener() {
    
    public void onClick(View v) {    
                String str = tv.getText().toString();     
    }
    

    设置EditText中的字符串:

    View row = recyclerView.getLayoutManager().findViewByPosition(0);
    EditText editText = (EditText)row.findViewById(R.id.edit_text); //pass the id of your EditText here
    editText.setText(str);
    

    【讨论】:

    • 已经试过了。但我不允许我这样做,因为我正在使用适配器,就像我说的那样。我告诉我“无法解析方法 findviewbyid”
    • findViewById 应该在视图上调用,所以从 RecyclerView 获取当前视图并使用它来获取 EditText。编辑答案,让我知道它是否有效。
    • 不,遗憾的是它不能这样工作,我已经尝试过了。我使用了我之前使用的 Recyclerview。我认为它不起作用,因为我使用了 3 个不同的 XML 类。
    猜你喜欢
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多