0
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;

        }

    }



}

在上面添加子方法的代码中,我应该写什么来再次添加子项????即如何添加编辑文本和微调器,就像它已经存在于 XML 中一样......请提出任何解决方案......

4

2 回答 2

2

如果您已经在 xml 中有 Spinner 和 EditText 并且想通过编程再次添加,那么首先从您的父布局中删除所有视图..

喜欢:

  parentlayout.removeAllViews();

然后为SpinnerEditText创建对象并添加到父布局

  EditText edt= new EditText(this);
  Spinner sp= new Spinner(this);
  parentlayout.addView(edt);
  parentlayout.addView(sp);
于 2013-09-20T09:29:45.633 回答
1

尝试这个:

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

Spinner spinner = new Spinner(this);
myLayout.addView(spinner);
于 2013-09-20T09:27:05.917 回答