我为布局Inflater等尝试了很多东西。 但无法访问切换按钮控件。 PostActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
View shareView = findViewById(R.id.in_shareLayout);
tbFb = (ToggleButton) shareView.findViewById(R.id.tbFb);
tbFb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save the state here
Log.e("I Am Tired!", "tired--> " + isChecked);
}
});
tbFb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("I Am Tired!", "tired--> ");
}
});
在上面的类中,onClick或者已检查已更改的侦听器未被触发。 它总是显示错误值
包含的布局sharecontent_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textSize="14dp"
android:text="share on"
/>
<ToggleButton
android:layout_width="39dp"
android:layout_height="39dp"
style="@style/fbtoggleButton"
android:background="@drawable/fbsharebg"
android:id="@+id/tbFb"/></LinearLayout>
这是我的布局PostActivity.xml
<LinearLayout
android:id="@+id/share_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="10"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<include
android:id="@+id/in_shareLayout"
layout="@layout/sharecontent_layout" />
</LinearLayout>
答案 0 :(得分:1)
您需要传递布局文件名。
改变你的:
layout="@layout/sharecontent_layout"
到
layout="@layout/ShareLayout"
另外
setContentView(R.layout.activity_post);
要
setContentView(R.layout.PostActivity);
答案 1 :(得分:0)
你无法以这种方式找到视图
View shareView = findViewById(R.id.in_shareLayout);
删除此
您可以直接访问随附的布局控件
tbFb = (ToggleButton) shareView.findViewById(R.id.tbFb);
答案 2 :(得分:-1)
使用此:
layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="share on"
android:textSize="14dp" />
<ToggleButton
android:id="@+id/tbFb"
android:layout_width="39dp"
android:layout_height="39dp" />
</LinearLayout>
layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include
android:id="@+id/in_shareLayout"
layout="@layout/layout1" />
</LinearLayout>
DemoActivity.java
public class DemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
ToggleButton tb = (ToggleButton) findViewById(R.id.tbFb);
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("Demo", "===================="+isChecked);
}
});
}
}
以上代码对我来说已经过测试了