都要遵照数据Curry的都会数目动态增进控件,能够保存以便在四个表单模板中重复使用

澳门新浦京8455com 5

使用duilib库写的界面,listUI加载一定数量的子控件,删除全部再添加这样的操作多次后,项目出现崩溃。不知道原因是啥啊?难道是list控件RemoveAll没删除干净导致的添加失败?

代码

一、准备工作:
1、LinkedList存储已添加的控件,制成索引,可配合数据库删除数据。

private LinkedList<Button> ListBtn_Add;
private LinkedList<TextView> ListText_Def;

2、获取手机屏幕的宽高,用于设置控件的宽高。

DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
width=dm.widthPixels;
height=dm.heightPixels;

3、用EDITSTATE判断是否可以删除控件和数据,为0时不能删除,为1时则相反

private int EDITSTATE=0

澳门新浦京8455com,4、因为不论是跳入WeatherAddActivity还是从ChooseAreaActivity退回到该Activity,都要根据数据库里的城市数据动态添加控件,所以我把程序都挪到了onStart()中写。

澳门新浦京8455com 1

图片来自网络.png

二、onStart()
1、清空LinkedList的所有数据,并删除所有动态添加的控件,再根据数据库添加控件

ListText_Def.clear();
ListBtn_Add.clear();
linearLayout.removeAllViews();
List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
for(County_Addition countyAddition:countyAdditions){//动态添加数据库中所有数据
        addBtn(countyAddition.getWeatherName(),countyAddition.getDef());
}

2、动态添加控件:

private void addBtn(String countyName, Boolean Def_flag){
        //添加外部Layout
        LinearLayout linear_btn = new LinearLayout(this);
        linear_btn.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams liParam = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linear_btn.setLayoutParams(liParam);
        //添加按钮
        Button btnAdd=new Button(this);
        LinearLayout.LayoutParams btnAddPar=new LinearLayout.LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, height/8,3);
        btnAddPar.setMargins(0,10,5,10);
        btnAdd.setLayoutParams(btnAddPar);
        btnAdd.setText(countyName);
        btnAdd.setTextColor(Color.argb(255, 255, 255, 255));
        btnAdd.setBackgroundColor(Color.argb(136, 0, 0, 0));
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(EDITSTATE==1)
                    delBtn(view);
            }
        });
        linear_btn.addView(btnAdd);
        ListBtn_Add.add(btnAdd);
        //添加TextView
        TextView textDef=new TextView(this);
        LinearLayout.LayoutParams textDefPar = new LinearLayout.LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, height/8, 1);
        textDefPar.setMargins(0, 10, 0, 10);
        textDef.setLayoutParams(textDefPar);
        if(Def_flag==true){
            textDef.setBackgroundColor(Color.argb(255, 171, 52, 56));
            textDef.setText("默认");
        }else if(Def_flag==false){
            textDef.setText("设为默认");
            textDef.setBackgroundColor(Color.argb(136, 0, 0, 0));
        }
        textDef.setGravity(Gravity.CENTER);
        textDef.setTextColor(Color.argb(255, 255, 255, 255));
        linear_btn.addView(textDef);
        textDef.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int k=0;
                for(int i=0;i<ListText_Def.size();i++){
                    ListText_Def.get(i).setBackgroundColor(Color.argb(136, 0, 0, 0));
                    ListText_Def.get(i).setText("设为默认");
                    if(ListText_Def.get(i)==view){
                        view.setBackgroundColor(Color.argb(255, 171, 52, 56));
                        ListText_Def.get(i).setText("默认");
                        k=i;
                    }
                }
                County_Addition countyAddition=new County_Addition();
                countyAddition.setToDefault("Def");
                countyAddition.updateAll();
                County_Addition countyAddition1=new County_Addition();
                countyAddition1.setDef(true);
                countyAddition1.updateAll("WeatherName = ?",ListBtn_Add.get(k).getText().toString());
            }
        });
        ListText_Def.add(textDef);
        linearLayout.addView(linear_btn);
    }

3、动态删除控件和数据:

private void delBtn(View view){
        int countyid;
        Boolean flag=false;
        if(view==null){
            return;
        }
        int iIndex=-1,position=0;
        for(int i=0;i<ListBtn_Add.size();i++){
            if(ListBtn_Add.get(i)==view){
                iIndex=i;
                break;
            }
        }
        if(iIndex>=0){
            List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
            for(County_Addition countyAddition:countyAdditions){
                if(position==iIndex){
                    countyid=countyAddition.getId();
                    Log.d("TAG",countyid+"");
                    if(countyAddition.getDef()==true){
                        flag=true;
                    }
                    DataSupport.delete(County_Addition.class,countyid);
                    break;
                }
                position++;
            }
            ListBtn_Add.remove(iIndex);
            ListText_Def.remove(iIndex);
            Log.d("TAG",!ListBtn_Add.isEmpty()+"");
            if(flag&&(!ListBtn_Add.isEmpty())){
                Log.d("TAG","First");
                ListText_Def.get(0).setText("默认");
                ListText_Def.get(0).setBackgroundColor(Color.argb(255, 171, 52, 56));
                County_Addition countyAddition=new County_Addition();
                countyAddition.setDef(true);
                countyAddition.updateAll("WeatherName = ?",ListBtn_Add.get(0).getText().toString());
            }
            linearLayout.removeViewAt(iIndex);
        }
    }

3、点击事件:

btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(EDITSTATE==0){
                    btn_edit.setText("确定");
                    EDITSTATE=1;
                }else if(EDITSTATE==1){
                    btn_edit.setText("编辑");
                    EDITSTATE=0;
                }
            }
        });
btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(WeatherAddActivity.this,ChooseAreaActivity.class);
                SharedPreferences.Editor editor= PreferenceManager.
                        getDefaultSharedPreferences(WeatherAddActivity.this).edit();
                editor.putBoolean("Where",true);
                editor.apply();
                linearLayout.removeAllViews();
                startActivity(intent);
            }
        });
btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断数据库的数据是否为空
                List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
                for(County_Addition countyAddition:countyAdditions){
                    number++;
                }
                if(number==0){
                    Toast.makeText(WeatherAddActivity.this,"城市不能为空,请添加城市",Toast.LENGTH_SHORT).show();
                }else{
                    number=0;
                    finish();
                }
            }
        });

NEXT:
酷欧天气2.0(四)——显示城市天气数据WeatherActivity

添加依赖

dependencies {

​ compile
‘com.android.support.constraint:constraint-layout:1.0.0-beta4’

}

一、新建一个一个空白表单模板部件,保存一下,记下路径。
澳门新浦京8455com 2

button靠右下角

澳门新浦京8455com 3

右下角

 

基础用法

  • 上下左右约束,每个控件的约束都分为垂直和水平两类,一共可以在四个方向上给控件添加约束:

    ###### button居中

澳门新浦京8455com 4

居中

澳门新浦京8455com 5

相关文章

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图