我的标题中有一个编辑按钮。我在循环中填充 TableLayout 。在循环中,我正在膨胀删除按钮 以下是我使用的代码:
MainActivity :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cookbooklist);
btnHeaderEdit = (Button)findViewById(R.id.btnEdit);
btnHeaderEdit.setVisibility(View.VISIBLE);
delete_button = (View)findViewById(R.id.btnDelete);
listTable = (TableLayout)findViewById(R.id.cookbookListTable);
btnHeaderEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
delete_button.getTag();
delete_button.setVisibility(View.VISIBLE);
}
});
for(int i=0;i<=10;i++)
{
// Row to display news title
final TableRow newsRow = new TableRow(this);
newsRow.setMinimumHeight(200);
newsRow.setClickable(true);
newsRow.setTag(i);
newsRow.setBackgroundColor(Color.WHITE);
// Some other views
//create inflater
LayoutInflater inflater = getLayoutInflater();
delete_button = inflater.inflate(R.layout.deletebutton,
(ViewGroup) newsRow, false);
delete_button.setTag(i);
delete_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listTable.removeView(newsRow);
}
});
//Add views in table
newsRow.addView(delete_button);
listTable.addView(newsRow);
}
}
delebutton.xml :
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btnDelete"
android:layout_width="100dip"
android:visibility="invisible"
android:layout_height="40dip"
android:padding="10sp"
android:text="Delete">
</Button>
问题:
我遇到的问题是,当我点击标题中的修改按钮时,删除按钮只会出现在最后一行。当我在布局中将删除按钮的可见性设置为可见时,它将显示在所有行中。但我希望它在开始时是隐形的。点击修改按钮后,它应显示在所有行中。我想知道如何实现这一目标 有人可以帮我吗?如果需要更多代码,请告诉我。
由于 我面临的问题是什么时候
答案 0 :(得分:0)
Hi Stone我认为你必须使用布尔标志。哪个将确定删除按钮是否应该可见。尝试使用以下示例。它的工作正常。如果删除按钮不可见,单击编辑按钮我会将所有内容视为可见,反之亦然。请尝试以下代码。
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sample2 extends Activity {
boolean showDeleteButtonFlag = false;
Button delteBtn2 = null;
Button delteBtn1 = null;
Button editBtn = null;
ArrayList<Button> deleteButtonList = new ArrayList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tablerow);
delteBtn1 = (Button) findViewById(R.id.delete1);
delteBtn2 = (Button) findViewById(R.id.delete2);
// add all delete buttons buttons to the array list
deleteButtonList.add(delteBtn1);
deleteButtonList.add(delteBtn2);
editBtn = (Button) findViewById(R.id.edit);
setVisibilityOfDeleteButtons();
editBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// if delete button is visible make it as invisible and viceversa
showDeleteButtonFlag = !showDeleteButtonFlag;
setVisibilityOfDeleteButtons();
}
});
}
void setVisibilityOfDeleteButtons(){
if(showDeleteButtonFlag){
Iterator<Button> itr =deleteButtonList.iterator();
while(itr.hasNext()){
itr.next().setVisibility(View.VISIBLE);
}
}else{
Iterator<Button> itr =deleteButtonList.iterator();
while(itr.hasNext()){
itr.next().setVisibility(View.INVISIBLE);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:id="@+id/edit"
android:layout_width="100dip"
android:layout_height="60dip"
android:text="Edit" />
<Button
android:id="@+id/delete1"
android:layout_width="100dip"
android:layout_height="60dip"
android:text="Delete" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textColor="#ffffff"
android:textStyle="bold" />
<Button
android:id="@+id/delete2"
android:layout_width="100dip"
android:layout_height="60dip"
android:text="Delete" />
</TableRow>
</TableLayout>