本文介绍了Android:如何在软键盘上的候选视图上创建按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按钮内创建候选人视图,但
I want to make candidateView inside button but
您看到了日志猫:
请分享代码
我的代码 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
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>
感谢前进
推荐答案
查看此行
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
您正在将TextView投射到LinearLayout中
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
是TextView
R.id.words
is a TextView
向您的LinearLayout添加ID
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>
并使用该ID
LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.wordsLayout);
这篇关于Android:如何在软键盘上的候选视图上创建按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!