Android:如何在软键盘的候选视图中创建按钮?

4
我希望在按钮内部创建candidateView,但是您可以看到日志记录:

enter image description here

请分享代码
我的代码 SoftKeyboard.java
@Override
    public View onCreateCandidatesView() {
        LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View wordBar = li.inflate(R.layout.wordbar, null);
        LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
        Button btn = (Button) wordBar.findViewById(R.id.button1);
        btn.setOnClickListener(this);
        mCandidateView = new CandidateView(this);
        mCandidateView.setService(this);
        setCandidatesViewShown(true);
        mCandidateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        ll.addView(mCandidateView);
        return wordBar;
    }

我想要做成这样:

enter image description here

布局 wordBar.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/words"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

提前感谢


你能给我们展示一下你的布局吗? - Emil
1个回答

2

请看这行文字

LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);

您将一个TextView转换为LinearLayout。
 <TextView
        android:id="@+id/words"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

R.id.words 是一个 TextView。

解决方法

在您的 LinearLayout 中添加一个 id。

例如:android:id="@+id/wordsLayout"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/wordsLayout"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/words"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

使用该ID

 LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.wordsLayout);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接