如何在android中将弹出框中的文本设置为特定的文本视图?
我在我的应用程序中创建了两个动态按钮和一个动态文本。现在我想在单击按钮(编辑)时更改如何在android中将弹出框中的文本设置为特定的文本视图?,android,button,android-alertdialog,Android,Button,Android Alertdialog,我在我的应用程序中创建了两个动态按钮和一个动态文本。现在我想在单击按钮(编辑)时更改文本视图中的文本。当我单击编辑按钮时,弹出框将出现,我将在该框中插入一些文本,并且该文本应放置到相应的文本视图中。我该怎么做?我已经粘贴了我的代码 公共类CreateButton扩展ActivityGroup实现View.OnClickListener { int i=0,delet_id=0,tr_id=0,edit_id=0,no=1,tv_id=0,tg=1; 餐桌布置图:TLU床、TLU厨房、TLU生活、
文本视图中的文本。当我单击编辑按钮时,弹出框将出现,我将在该框中插入一些文本,并且该文本应放置到相应的文本视图中。我该怎么做?我已经粘贴了我的代码
公共类CreateButton扩展ActivityGroup实现View.OnClickListener
{
int i=0,delet_id=0,tr_id=0,edit_id=0,no=1,tv_id=0,tg=1;
餐桌布置图:TLU床、TLU厨房、TLU生活、TLU餐饮;
TableRow tr;
按钮btn_删除、编辑;
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();
}
}
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tl_床=(表格布局)findViewById(R.id.tableLayoutOfBed);
按钮Btn_卧室=(按钮)findViewById(右id床);
Btn_卧房。setOnClickListener(卧房的ListenrOf);
}
//创建卧室的侦听器
private OnClickListener ListenerOf_卧房=新建OnClickListener(){
@凌驾
公共void onClick(视图v){
int no=3;
电视=新文本视图[否];
for(int i=0;i
}公共类CreateButton扩展ActivityGroup实现View.OnClickListener
{
int i=0;
床的布局图;
TableRow tr;
按钮btn_删除、编辑;
TextView[]tv;//这里我已经清除了一个TextView数组
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tl_床=(表格布局)findViewById(R.id.tableLayoutOfBed);
按钮Btn_卧室=(按钮)findViewById(右id床);
Btn_卧房。setOnClickListener(卧房的ListenrOf);
}
//创建卧室的侦听器
private OnClickListener ListenerOf_卧房=新建OnClickListener(){
@凌驾
公共void onClick(视图v){
int no=3;
电视=新文本视图[否];
for(int i=0;ipublic 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();
}
}
}