我想在候选内部按钮,但是
你看日志猫:
请分享代码
我的守则
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>
谢谢你提前
解决方法:
看到这一行
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
解
将ID添加到LinearLayout
示例: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);