我是初学者。
我想从edittext中提供值并将其设置为按钮文本。 当编辑文本和按钮处于不同的活动中时(可以使用意图和附加内容),我可以这样做,但是当他们处于相同的活动时呢?
这是我到目前为止的代码(我使用对话框获取edittext值,edittext在“addalbumsdialog”布局中):
public class Albums extends Activity {
Button b1;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button b2 = (Button) findViewById(R.id.addalbum);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1 = (Button) findViewById(R.id.album1);
b1.setText(et1.getText().toString());
b1.setVisibility(View.VISIBLE);
}
});
myAlertDialog.show();
}
}
感谢。
答案 0 :(得分:1)
您需要更改
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
将Button声明为实例变量
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
b2 = (Button) findViewById(R.id.addalbum);
然后点击按钮
@Override
public void onClick(DialogInterface arg0, int arg1) {
b2.setText(et1.getText().toString();
}
});
如果按钮b1属于充气的视图,则需要将文本设置为
然后
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
b1 = (Button) view.findViewById(R.id.album1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1.setVisibility(View.VISIBLE);
b1.setText(et1.getText().toString());
}
});
myAlertDialog.show();
}
您尚未提及要更改的按钮文字。所以发布了两个
答案 1 :(得分:1)
你应该替换这个
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
使用
View view = (View) inflater.inflate(R.layout.addalbumsdialog, null);
myAlertDialog.setView(view);
et1 = (EditText) view.findViewById(R.id.editText1);
b1 = (Button) view.findViewById(R.id.album1);
myAlertDialog.setTitle("Name?");
将create Button b2
添加为实例变量,如:
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
b2 = (Button) findViewById(R.id.addalbum);
然后点击按钮
@Override
public void onClick(DialogInterface arg0, int arg1) {
b2.setText(et1.getText().toString();
}
});
答案 2 :(得分:0)
替换像这样的代码
public class Albums extends Activity {
Button b1;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.album);
Button b2 = (Button) findViewById(R.id.addalbum);
b1 = (Button) findViewById(R.id.album1);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
et1 = (EditText) findViewById(R.id.editText1);
myAlertDialog.setTitle("Name?");
myAlertDialog.setView(inflater.inflate(R.layout.addalbumsdialog, null));
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
b1.setText(et1.getText().toString());
}
});
myAlertDialog.show();
}
}