1、定义接口content的方法,如ok,cancle;
2、在View的处理类myview中实现content的方法。
3、通过contently.getChildAt(i)的方法获得View v;
4、可以使用 content ct = (content)v;
5、使用ct.ok()和ct.cancel();
代码如下:
ll = (LinearLayout) findViewById(R.id.contently);
btncancle = (Button) findViewById(R.id.buttoncancel);
btnok = (Button) findViewById(R.id.buttonok);
View view = (View) ll.getChildAt(0);
Toast.makeText(getApplicationContext(),String.valueOf(view.getId()) , Toast.LENGTH_LONG).show();
final myview mv=(myview)view;//这里也无法运行
//content ct=(content)view;也不行
// Toast.makeText(getApplicationContext(),ct.getClass().toString() , Toast.LENGTH_LONG).show();
btncancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// ct.cancel();
mv.cancel();
}
});
btnok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// ct.ok();
mv.ok();
}
});
}
public class myview extends View implements content{
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public myview(Context context,AttributeSet attr){
super(context,attr);
}
@Override
public void cancle() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"cancel" , Toast.LENGTH_LONG).show();
}
@Override
public void ok() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"ok", Toast.LENGTH_LONG).show();
}
}
public interface content {
public void ok();
public void cancel();
}
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="50dp"
android:textColor="#f00"
android:id="@+id/text" />
android:animateLayoutChanges="true"
android:id="@+id/tianjiachu"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0px">
</LinearLayout>
<TextView
android:onClick="anjian"
android:id="@+id/xxx"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/baise"
android:text="测试"
android:textSize="20dp"/>
<TextView
android:onClick="anjian"
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/baise"
android:text="添加"
android:textSize="20dp"/>
int a = 0;
private void tianjia() {
LinearLayout tianjiachu = (LinearLayout)findViewById(R.id.tianjiachu);
View view = LayoutInflater.from(this).inflate(R.layout.你XML文件取得名字,null,false );
TextView text = (TextView)view.findViewById(R.id.text);
text.setText("ID"+a);
a++;
}
public void anjian(View v) {
switch (v.getId()) {
case R.id.add: tianjia();
break;
case R.id.xxx:
LinearLayout XX = (LinearLayout)findViewById(R.id.tianjiachu); //先找到 你要操作的 布局文件
View cccccccccccc =XX.getChildAt(99999999999); //99999999999 你想改变 第几行 或者列 或者删除 随便你 不过你得有id ID相同不是问题
TextView text = (TextView)cccccccccccc.findViewById(R.id.text);
text.setText("行不行啊");
break;
}
这应该是最简单的 动态添加操作了 也应该是 最简单的 编写了 至于怎么知道自己要操作 第几行 第几列 ?
**我这个方法是用来 滑动分页 的 HorizontalScrollView + LinearLayout 实现滑动分页 **
好了 不扯了 博主的 xxxxxxxxx.getChildAt(00000000000000000000000000000) 给了我最大的帮助 在此先谢过了