我是 Android 世界的新手,我有以下情况:
我有一个 Activity main.xml RelativeLayout 和一个 ExpandableListView。
当我展开一个组时,它会向我显示孩子的内容:这是 example.xml。
现在我在 example.xml-layout 1 上有 id 为 btn_send 的按钮,我的问题是如何访问该按钮?
我尝试通过 Example.java 访问按钮,但出现错误,无法找到 btn_send。我通过 Main.java 尝试了一些错误。
先感谢您,
BR阿里
public class Main extends Activity {
ExpandableListView exv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
exv = (ExpandableListView) findViewById(R.id.exv_main);
exv.setAdapter(new MainAdapter(this));
exv.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition != previousGroup)
exv.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
}
}
在 MainAdapter 类中
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = new RelativeLayout(context);
ImageView iv = new ImageView(context);
RelativeLayout.LayoutParams llpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.setGravity(Gravity.CENTER | Gravity.TOP);
switch (groupPosition){
case 0:
layout = (RelativeLayout) inflater.inflate(childList[groupPosition][childPosition], null);
break;
case 1:
break;
case 2:
break;
}
return layout;
}
和 main.xml
<ExpandableListView
android:id="@+id/exv_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:background="#CDCDD0"
android:divider="@android:color/transparent"
android:listSelector="#CDCDD0"
android:dividerHeight="1dip"
android:cacheColorHint="#CDCDD0"
android:childDivider="#CDCDD0"
>
</ExpandableListView>
并在 example.xml
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send" />
并在 Example.java
公共类示例扩展活动{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
final Button btn = (Button) findViewById(R.id.btn_send);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn.setText("huhu");
}
});
}
}