在android中动态创建的edittext上设置文本?

时间:2012-02-18 06:56:11

标签: android dynamic button android-edittext

hai是android中的新手我创建了一个动态按钮并编辑了文本创建我有两个页面1用于输入应该创建多少个按钮和编辑文本。 (在该页面中我只有一个编辑文本字段和按钮,如果我输入3。三个edittext和3个按钮将在下一页动态创建)如果我按三个下一页将像三个按钮和三个edittext

我想在按输入ur1st time按钮的第一个编辑文本上打印你好,但是当我按下那个按钮时,值会打印在第三个edittext上 代码如下:

package a.l;


   public class dyna extends Activity {
TextView t1;
Button btn;
EditText edt;
int i,j,t = 0,mHour,mMinute,count=0,mDate,mYear,mMonth;
Button b;
Toast mToast;
ScrollView sv;
Calendar c = Calendar.getInstance();
SQLiteDatabase myDB ; 
PendingIntent sender;
AlarmManager am;
Intent intent;
int alarmtype;
long timeinmilli;



protected static final int TIME_DIALOG_ID = 1;
 private static final int DATE_DIALOG_ID = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dyna);
t1 = (TextView)findViewById(R.id.textdyna);


 mHour = c.get(Calendar.HOUR_OF_DAY);
 mMinute = c.get(Calendar.MINUTE);
 mDate=c.get(Calendar.DATE);
 mMonth=c.get(Calendar.MONTH);
 mYear=c.get(Calendar.YEAR);
 String date=mDate+"/"+mMonth+"/"+mYear;
 count=1;



        Bundle bu =  getIntent().getExtras();
        String s1 = bu.getString("5");
        int i1=Integer.parseInt(s1);
        String s2 = bu.getString("3");
        String s3 = bu.getString("4");




//  if(date.equals(s2)){     
        sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setBackgroundResource(R.drawable.vec);


        sv.addView(ll);

        for(i = 1; i <i1+1; i++)
        {
           b = new Button(this);

           b.setText(" enter ur"+ i +"st time");
           b.setId(i); 
           b.setTextSize(10);
           b.setPadding(8,3,8,3);   
           b.setTextColor(Color.DKGRAY);
           b.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);     
           b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));        


           c.setTimeInMillis(System.currentTimeMillis());
            c.add(Calendar.SECOND,10);
            alarmtype=AlarmManager.RTC_WAKEUP;
            timeinmilli= c.getTimeInMillis();
           intent = new Intent(dyna.this, MyAlarmReceiver.class);
           sender = PendingIntent.getBroadcast(dyna.this,0, intent, 0);
           am = (AlarmManager)getSystemService(ALARM_SERVICE);



           edt=new EditText(this);
           edt.setId(i);
           edt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));





           ll.addView(b);
           ll.addView(edt);
           System.out.println("am on");

           b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int a=edt.getId();
                    int bh=b.getId();





                    //showDialog(TIME_DIALOG_ID);

                System.out.println("am on button");    

                        // We want the alarm to go off 10 seconds from now.


                      // c.add(Calendar.MINUTE,mMinute);


                        // Schedule the alarm!

   //                           am.set(alarmtype,timeinmilli, sender);
   //
  //                       
   //                           // Tell the user about what we did.
  //                            if (mToast != null) {
  //                                mToast.cancel();
  //                            }
  //                            mToast = Toast.makeText(dyna.this,"Alarm    Started on"+i,Toast.LENGTH_LONG);
  //                            mToast.show();
   //       
                       }
            });



        }



        this.setContentView(sv);    

//  }
  //      do it for ending alarm at this date
   ////   
   //////     if(date.equals(s3))
  ////////  {
  //////          
 ////     }
  //      

}

 private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;


            }


        };
        protected Dialog onCreateDialog(int id) {
            // TODO Auto-generated method stub
            switch (id) {


             case TIME_DIALOG_ID:
                return new TimePickerDialog(this,
                         mTimeSetListener, mHour, mMinute, false);


                        }
            return null;
        }
}

我正在使用的代码请找一个解决方案谢谢

2 个答案:

答案 0 :(得分:1)

试用此代码

package com.openxcell.DemoAnimation;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class **DynamicEditText** extends Activity implements OnClickListener {

    ArrayList<Integer> _ArrListScpoe = new ArrayList<Integer>();
    // List<EditText> allEds = new ArrayList<EditText>();
    List<Button> allbtn = new ArrayList<Button>();
    RelativeLayout.LayoutParams ParaOne;
    EditText EtScopeDYN, EtBidDYN;
    Button btnDynamic, btnDynamicOne, btnOne;
    int _intClick = 0, _intClickBid = 0;
    LinearLayout LLAdd, LLDynamic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext);
        LLAdd = (LinearLayout) findViewById(R.id.LLAdd);
        LLAdd.setOrientation(LinearLayout.VERTICAL);

        btnOne = (Button) findViewById(R.id.button1);
        btnOne.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                DynamicEditScope();
            }
        });
    }

    public void DynamicEditScope() {
        ParaOne = new RelativeLayout.LayoutParams(120, 40);
        EtScopeDYN = new EditText(DynamicEditText.this);
        LLDynamic = new LinearLayout(DynamicEditText.this);
        btnDynamic = new Button(DynamicEditText.this);

        EtScopeDYN.setId(_intClick);
        // allEds.add(EtScopeDYN);
        EtScopeDYN.setTextSize(12);
        ParaOne.topMargin = 5;
        ParaOne.leftMargin = 55;

        btnDynamic.setText("Add" + _intClick);
        btnDynamic.setId(_intClick);
        allbtn.add(btnDynamic);
        btnDynamic.setOnClickListener(this);

        LLDynamic.addView(EtScopeDYN, ParaOne);
        LLDynamic.addView(btnDynamic, ParaOne);
        LLDynamic.setOrientation(LinearLayout.HORIZONTAL);

        LLAdd.addView(LLDynamic);
        _ArrListScpoe.add(_intClick);
        _intClick++;
    }

    public void onClick(View v) {
        for (int i = 0; i < _ArrListScpoe.size(); i++) {
            if (v.getId() == i) {
                EtBidDYN = (EditText) findViewById(_ArrListScpoe.get(i));
                EtBidDYN.setText(allbtn.get(i).getText().toString());
            }

        }

    }

}

答案 1 :(得分:0)

ButtonEditText创建单独的数组...以便您可以轻松识别。另外,我可以看到您为ButtonEditText设置了相同的ID。

在数组内部,只需初始化数组并在setOnClickListener的{​​{1}}内,设置相应的Button