如何将弹出框中的文本设置为Android中的特定文本视图?

| 我在应用程序中创建了两个动态按钮和一个动态文本。现在我想在单击按钮(编辑)时更改
TextView
中的文本。当我单击编辑按钮时,将出现弹出框,在该框中我将插入一些文本,然后将该文本放置在适当的“ 0”上。我怎样才能做到这一点?我已经粘贴了代码。 公共类CreateButton扩展ActivityGroup实现View.OnClickListener {     int i = 0,delet_id = 0,tr_id = 0,edit_id = 0,no = 1,tv_id = 0,tg = 1;     TableLayout tl_bed,tl_kitchen,tl_living,tl_dining;     TableRow tr;     按钮btn_delete,edit;     TextView []电视;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tl_bed = (TableLayout)findViewById(R.id.tableLayoutOfBed);

    Button Btn_BedRoom = (Button) findViewById(R.id.bed);
    Btn_BedRoom.setOnClickListener(ListenrOf_BedRoom);


}

// Listener Of Create Bed Room
private OnClickListener ListenrOf_BedRoom = new OnClickListener() {
    @Override
    public void onClick(View v) {
        int no = 3;
        tv = new TextView[no];
        for(int i =0 ;i< no  ;i++){

        CreateRoom(i);
        }
    }
};

public void CreateRoom(int i)
{
    tr = new TableRow(this);
    tr.setId(tr_id);
    TableRow.LayoutParams Rdel = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT);

    Rdel.setMargins(45, 10, 0,0);

    Button btn_delete = new Button(this);

    btn_delete.setText(\"delete\");
    btn_delete.setHeight(10);
    btn_delete.setWidth(10);
    btn_delete.setBackgroundResource(R.drawable.close_btn);
    btn_delete.setLayoutParams(Rdel);
    btn_delete.setTag(\"DeleteTag\");
    btn_delete.setId(delet_id);
    tr.addView(btn_delete);
    btn_delete.setOnClickListener(this);

    Rdel.setMargins(15, 10, 0,0);
    Button edit = new Button(this);

    edit.setBackgroundResource(R.drawable.edit_btn);
    edit.setHeight(10);
    edit.setWidth(10);
    edit.setLayoutParams(Rdel);
    edit.setTag(\"Edit Name\");
    edit.setId(edit_id);
    tr.addView(edit);
    edit.setOnClickListener(this);

    TableRow.LayoutParams tr_text = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.MATCH_PARENT);
    tr_text.setMargins(25, 5, 0,0);
    tv[i] = new TextView(this);

    tv[i].setLayoutParams(tr_text);
    tv[i].setId(tv_id);
   // tv.setTag(\"Change Text\"+tg++);
    tv[i].setText(\"Bed Room\"+no++);
    tv[i].setEnabled(true);

    tr.addView(tv[i]);

    tl_bed.addView(tr);

    delet_id++;
    tr_id++;
    edit_id++;
    tv_id++;
}

public void onClick(final View v) 
{
  final String tag = (String) v.getTag();
  final int z = v.getId();
  //Edit Click
  if (tag == \"Edit Name\") 
    {
        // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        // set the message to display
        alertbox.setCancelable(true);
        // alertbox.setTitle(\"Hi Akshay\"); // Gives Msg
        alertbox.setMessage(\"Edit Room Name\"); // Gives Msg same as above

        final EditText et = new EditText(this);
        et.setId(edit_id++);
        final int a = et.getId();
        alertbox.setView(et);



        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton(\"Save\",
                new DialogInterface.OnClickListener() {

        // click listener on the alert box
    public void onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
                    final String s = et.getText().toString();
                        tv[z].setText(s);

                                                Toast.makeText(getApplicationContext(),\"Name Changed\", Toast.LENGTH_LONG).show();
                    }

                                            }
                });
        // show it
        alertbox.show();
    }

    if( tag == \"DeleteTag\") 
    {
        int tr = v.getId();
        tl_bed.removeView(findViewById(tr));
        Toast.makeText(getApplicationContext(),\"Room Deleted \" +(tr+1), Toast.LENGTH_LONG).show();

    }
}         
}     
已邀请:
        
public class CreateButton extends ActivityGroup implements View.OnClickListener
{

int i=0;
TableLayout tl_bed;
TableRow tr;
Button btn_delete,edit;
TextView[] tv; // Here i have decleared an array of TextView


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tl_bed = (TableLayout)findViewById(R.id.tableLayoutOfBed);

    Button Btn_BedRoom = (Button) findViewById(R.id.bed);
    Btn_BedRoom.setOnClickListener(ListenrOf_BedRoom);      
}

// Listener Of Create Bed Room
private OnClickListener ListenrOf_BedRoom = new OnClickListener() {
    @Override
    public void onClick(View v) {
        int no = 3;
        tv = new TextView[no];
        for(int i =0 ;i< no  ;i++){

        CreateRoom(i);
        }
    }
   };

   public void CreateRoom(int i)
{
    tr = new TableRow(this);
 // Here i have set the same id for deleting row and editing name.
    tr.setId(i);
    TableRow.LayoutParams Rdel = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT);

    Rdel.setMargins(45, 10, 0,0);

    Button btn_delete = new Button(this);

    btn_delete.setText(\"delete\");
    btn_delete.setHeight(10);
    btn_delete.setWidth(10);
    btn_delete.setBackgroundResource(R.drawable.close_btn);
    btn_delete.setLayoutParams(Rdel);
    btn_delete.setTag(\"DeleteTag\");
    btn_delete.setId(i);
    tr.addView(btn_delete);
    btn_delete.setOnClickListener(this);

    Rdel.setMargins(15, 10, 0,0);
    Button edit = new Button(this);

    edit.setBackgroundResource(R.drawable.edit_btn);
    edit.setHeight(10);
    edit.setWidth(10);
    edit.setLayoutParams(Rdel);
         edit.setTag(\"Edit Name\");
         edit.setId(i);
    tr.addView(edit);
    edit.setOnClickListener(this);

    TableRow.LayoutParams tr_text = new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.MATCH_PARENT);
    tr_text.setMargins(25, 5, 0,0);
    tv[i] = new TextView(this);

    tv[i].setLayoutParams(tr_text);
    tv[i].setId(i);
    tv[i].setText(\"Bed Room\"+no++);
    tv[i].setEnabled(true);

    tr.addView(tv[i]);

    tl_bed.addView(tr);

  }

public void onClick(final View v) 
{
  final String tag = (String) v.getTag();
  final int z = v.getId();
  //Edit Click
  if (tag == \"Edit Name\") 
    {
        // prepare the alert box
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        // set the message to display
        alertbox.setCancelable(true);
        // alertbox.setTitle(\"Hi Akshay\"); // Gives Msg
        alertbox.setMessage(\"Edit Room Name\"); // Gives Msg same as above

        final EditText et = new EditText(this);
        et.setId(edit_id++);
        final int a = et.getId();
        alertbox.setView(et);



        // add a neutral button to the alert box and assign a click listener
        alertbox.setNeutralButton(\"Save\",
                new DialogInterface.OnClickListener() 
    {

    // click listener on the alert box
    public void onClick(DialogInterface arg0, int arg1) {
        // the button was clicked
    final String s = et.getText().toString();
    tv[z].setText(s);
                                        Toast.makeText(getApplicationContext(),\"Name Changed\", Toast.LENGTH_LONG).show();
                    }


                    }
                });
        // show it
        alertbox.show();
    }

    if( tag == \"DeleteTag\") 
    {
        int tr = v.getId();
        tl_bed.removeView(findViewById(tr));
        Toast.makeText(getApplicationContext(),\"Room Deleted \" +(tr+1), Toast.LENGTH_LONG).show();

    }
}         
   }
这是我更新的课程,现在可以正常工作。     

要回复问题请先登录注册