Android: How to make button on candidate view in softkeyboard?

此生再无相见时 提交于 2019-12-05 01:16:03

问题


I want to make candidateView inside button but

you see log cat:

please share code

My Code 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;
    }

I want to make like this:

Layout 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>

Thanks for advance


回答1:


See this line

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

You are casting a TextView into LinearLayout

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

R.id.words is a TextView

Solution

Add an id to your LinearLayout

Example :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>

And use that id

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


来源:https://stackoverflow.com/questions/32450743/android-how-to-make-button-on-candidate-view-in-softkeyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!