【问题标题】:Android: How to make button on candidate view in softkeyboard?Android:如何在软键盘的候选视图上制作按钮?
【发布时间】:2015-12-03 17:19:57
【问题描述】:

我想在按钮内制作候选视图,但是

你看日志猫:

请分享代码

我的代码 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;
    }

我想做这样的:

布局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>

感谢提前

【问题讨论】:

  • 你能告诉我们你的布局吗?

标签: android android-softkeyboard ime candidate


【解决方案1】:

看到这一行

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 是一个文本视图

解决方案

为您的 LinearLayout 添加一个 id

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>

并使用该 ID

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

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 2011-08-25
    • 2013-02-25
    • 1970-01-01
    • 2017-02-23
    • 2013-04-10
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多