2011-06-25 23 views
0

私のアプリケーションで2つのダイナミックボタンと1つのダイナミックテキストを作成しました。ここでTextViewのテキストを変更したいのですが、ボタンをクリックしたときに編集ボタンをクリックするとのポップアップボックスが来ますそのボックスにテキストを挿入し、そのテキストを適切なTextViewに配置する必要があります。これどうやってするの?私は自分のコードを貼り付けました。ポップアップボックスのテキストをアンドロイドの特定のテキストビューに設定するにはどうすればいいですか?

CreateButtonはActivityGroupがView.OnClickListener {int型I = 0、delet_id = 0、tr_id = 0、edit_id = 0、なし= 1、tv_id = 0、TG = 1実装延びパブリッククラス;を TableLayout tl_bed、tl_kitchen、tl_living、tl_dining; TableRow tr; ボタンbtn_delete、編集; TextView [] tv;

/** 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(); 

    } 
}   

}

+0

ここで私はそれを見ることができないのですか? – Deva

+0

あなたのコードはここには表示されませんplz再びそれを投稿してください.. – Android

+0

あなたのコードを見ることができません。 – dhams

答えて

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

    } 
}   
    } 

これは私の更新されたクラスで、現在は正常に動作しています。

関連する問題