我在Android的世界新的,我有以下情形:ExpandableListView及子活動
我有一個活動的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");
}
});
}
}
請張貼您的代碼片段和XML佈局。 –
@AndreaCarrer感謝你的回答。但我不知道如何粘貼我的代碼片段:-(它要長! – Andy
粘貼代碼中最相關的部分,與btn_send對象連接的部分。 –