动态添加的意见和它敬酒 [英] dynamically adding views and toasting it

查看:139
本文介绍了动态添加的意见和它敬酒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在动态添加的看法。意思是说,最初有一个的EditText 按钮添加按钮提交

按钮的1.OnClick添加我在夸大新的布局具有一个的EditText 按钮删除。如果按钮添加是继续pressed将分别增加虚增布局。

按钮的2.OnClick提交有敬酒充气布局的EditText 值。

我只得到最后一个的EditText 烤面包和敬酒的休息的EditText 并没有表现出。如何做到这一点?

 公共类MainActivity扩展活动实现OnClickListener {
按钮添加,提交;
ArrayList的<视图> Viewlist产品;
的LinearLayout lin_layout;
ET1的EditText,ET2;
串S;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    ET1 =(EditText上)findViewById(R.id.editText1);
    添加=(按钮)findViewById(R.id.button_add);
    提交=(按钮)findViewById(R.id.bSubmit);
    lin_layout =(的LinearLayout)findViewById(R.id.linearLayout_view);
    Viewlist产品=新的ArrayList<视图>();
    add.setOnClickListener(本);
    submit.setOnClickListener(本);
}@覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.button_add:
        如果(et1.length()== 0){
            Toast.makeText(getApplicationContext(),
                    归档不能为空,0).show();
        }其他{
            LayoutInflater layoutInflater =(LayoutInflater)getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            最后查看addView = layoutInflater.inflate(
                    R.layout.inflate_layout,NULL);
            ET2 =(EditText上)addView.findViewById(R.id.edit);            viewList.add(addView);
            lin_layout.addView(addView);            按钮来删除=(按钮)addView
                    .findViewById(R.id.button_remove);
            remove.setOnClickListener(新View.OnClickListener(){                @覆盖
                公共无效的onClick(视图v){                    lin_layout.removeView((查看)v.getParent());
                    viewList.remove((查看)v.getParent());                }
            });        }
        打破;
    案例R.id.bSubmit:
        。S1 = et2.getText()的toString();
        Toast.makeText(getApplicationContext(),项目:+ S1,Toast.LENGTH_SHORT).show();
        打破;
    }} }


解决方案

解决的办法很简单,只要创建了EDITTEXT的实例在本地,它会工作:)
现在,一切都进展良好,但在使用引用旧的EditText问题创建

  @覆盖
公共无效的onClick(视图v){
    开关(v.getId()){
    案例R.id.button_add:
        如果(et1.length()== 0){
            Toast.makeText(getApplicationContext(),提起不能为空,0).show();
        }其他{
            LayoutInflater layoutInflater =(LayoutInflater)getBaseContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
            最后查看addView = layoutInflater.inflate(R.layout.inflate_layout,NULL);
            ET2的EditText =(EditText上)addView.findViewById(R.id.edit);
            viewList.add(addView);
            lin_layout.addView(addView);
            按钮来删除=(按钮)addView.findViewById(R.id.button_remove);
            remove.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    lin_layout.removeView((查看)v.getParent());
                    viewList.remove((查看)v.getParent());
                }
            });
        }
        打破;
    案例R.id.bSubmit:
        。S1 = et2.getText()的toString();
        Toast.makeText(getApplicationContext(),项目:+ S1,Toast.LENGTH_SHORT).show();
        打破;
    }
}

I'm adding views dynamically. Mean to say that, Initially there is one EditText, Button Add, Button Submit.

1.OnClick of Button Add I'm inflating new layout which has one EditTextand Button Remove. If Button Add is keep on pressed it will add inflated layout respectively.

2.OnClick of Button Submit it has to Toast the EditText values of Inflated layout.

I'm getting only the last EditText Toast, and the Toast for rest of EditText didn't show. how to do this?

public class MainActivity extends Activity implements OnClickListener {
Button add, submit;
ArrayList<View> viewList;
LinearLayout lin_layout;
EditText et1, et2;
String s1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1 = (EditText) findViewById(R.id.editText1);
    add = (Button) findViewById(R.id.button_add);
    submit = (Button) findViewById(R.id.bSubmit);
    lin_layout = (LinearLayout) findViewById(R.id.linearLayout_view);
    viewList = new ArrayList<View>();
    add.setOnClickListener(this);
    submit.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_add:
        if (et1.length() == 0) {
            Toast.makeText(getApplicationContext(),
                    "Filed cant be left empty", 0).show();
        } else {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(
                    R.layout.inflate_layout, null);
            et2 = (EditText) addView.findViewById(R.id.edit);

            viewList.add(addView);
            lin_layout.addView(addView);

            Button remove = (Button) addView
                    .findViewById(R.id.button_remove);
            remove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    lin_layout.removeView((View) v.getParent());
                    viewList.remove((View) v.getParent());

                }
            });

        }
        break;
    case R.id.bSubmit:
        s1 = et2.getText().toString();
        Toast.makeText(getApplicationContext(), "items: " + s1,Toast.LENGTH_SHORT).show();
        break;
    }

}

 }

解决方案

The solution is very simple, Just create the instance of the editText locally, it will work :) Now, everything is going fine but the problem creates when you use to reference old edittext

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_add:
        if (et1.length() == 0) {
            Toast.makeText(getApplicationContext(), "Filed cant be left empty", 0).show();
        } else {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.inflate_layout, null);
            EditText et2 = (EditText) addView.findViewById(R.id.edit);
            viewList.add(addView);
            lin_layout.addView(addView);
            Button remove = (Button) addView.findViewById(R.id.button_remove);
            remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    lin_layout.removeView((View) v.getParent());
                    viewList.remove((View) v.getParent());
                }
            });
        }
        break;
    case R.id.bSubmit:
        s1 = et2.getText().toString();
        Toast.makeText(getApplicationContext(), "items: " + s1, Toast.LENGTH_SHORT).show();
        break;
    }
}

这篇关于动态添加的意见和它敬酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆