当我在java代码中点击Add按钮时如何添加Edittext和Spinner?

时间:2013-09-20 09:23:44

标签: java android

package com.nyurals.school;



import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActitivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_settings );

        ActionBar actionBar = getActionBar();
        actionBar.show();
        actionBar.setTitle("SETTINGS");

        int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        TextView textView = (TextView)findViewById(titleId);
    //  textView.setText("ANNOUNCEMENTS");
        textView.setTextColor(android.graphics.Color.BLUE);

        actionBar.setDisplayHomeAsUpEnabled(true);

        init();
        super.onCreate(savedInstanceState);
    }

    private void init() {

        //textParentName = (TextView) findViewById(R.)
        String[] items = { "Nursery", "KG-I", "KG-II", "I", "II",
                "III", "IV", "V" };
        Spinner spinnerForClass = (Spinner) findViewById(R.id.spinnerClass);
        //      spinnerForClass.setOnItemSelectedListener(this);

        ArrayAdapter<String> spinnerClass = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,items);

        spinnerForClass.setAdapter(spinnerClass);
    }

    public void addChildren(View view) {


        Toast.makeText(SettingsActitivity.this, "Clicked on Add", Toast.LENGTH_SHORT).show();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_settings, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
        case R.id.item_save:
            Toast.makeText(SettingsActitivity.this, "Details saved.. Thank You!!", Toast.LENGTH_SHORT).show();
            return true;
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;

        default:
            return false;

        }

    }



}

在上面的代码中添加了Children方法,我该怎么写才能再次添加child?即如何添加编辑文本和微调器,就像它已经存在来自XML ... 请提出任何解决方案......

2 个答案:

答案 0 :(得分:2)

如果您已经在xml中安装了Spinner和EditText并想要通过问题语法再次添加,那么首先从父布局中删除所有视图。

像:

  parentlayout.removeAllViews();

之后为 Spinner EditText 创建对象并添加到父版式

  EditText edt= new EditText(this);
  Spinner sp= new Spinner(this);
  parentlayout.addView(edt);
  parentlayout.addView(sp);

答案 1 :(得分:1)

试试这个:

EditText myEdtTxt = new EditText(this);
myLayout.addView(myEdtTxt);

Spinner spinner = new Spinner(this);
myLayout.addView(spinner);