我有一个活动,我想将对话称为:
Intent intent = new Intent(MainActivity.this, start_dialog.class);
MainActivity.this.startActivity(intent);
我有 start_dialog 类:
public class start_dialog extends Dialog implements android.view.View.OnClickListener{
EditText Dialog1=(EditText)findViewyId(R.id.Dialog1);
EditText Dialog2=(EditText)findViewyId(R.id.Dialog2);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.dialog_view);
LayoutInflater li = LayoutInflater.from(getContext());
View promptsView = li.inflate(R.layout.dialog_view, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
alertDialogBuilder.setView(promptsView);
final EditText userInput1 = (EditText) promptsView
.findViewById(R.id.Dialog1);
final EditText userInput2 = (EditText) promptsView
.findViewById(R.id.Dialog2
);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Dialog1.setText(userInput1.getText());
Dialog2.setText(userInput2.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
我的问题是:
1)它给了我“必须定义一个显式的构造函数”,我不知道该放什么。
2)我不确定这是否有效。我希望对话框有 2 个编辑文本字段(和 2 个 texview),并且当用户输入填充列表视图时。
- - - - - - - - - - - - - - - - - 更新 - - - - - - - - ---------------
我的 start_dialog 是:
public class start_dialog extends Dialog {
public start_dialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_view);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.dialog_view);
//public void onClick(View v) {
LayoutInflater li = LayoutInflater.from(getContext());
View promptsView = li.inflate(R.layout.dialog_view, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput1 = (EditText) promptsView
.findViewById(R.id.Dialog1);
final EditText userInput2 = (EditText) promptsView
.findViewById(R.id.Dialog2);
final EditText Dialog1 = (EditText) findViewById(R.id.Dialog1);
final EditText Dialog2 = (EditText) findViewById(R.id.Dialog2);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
Dialog1.setText(userInput1.getText());
Dialog2.setText(userInput2.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
谢谢!