我有一个按钮,当我点击该按钮时会打开一个对话框。在该对话框中有一个EditText和ok按钮,当我单击该文本显示的ok按钮时,我输入一些文本 我不知道怎么会这样做,帮帮我
答案 0 :(得分:3)
使用以下
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private EditText result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// components from main.xml
button = (Button) findViewById(R.id.buttonPrompt);
result = (EditText) findViewById(R.id.editTextResult);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View view= li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(view);
final EditText userInput = (EditText) view
.findViewById(R.id.editTextDialogUserInput);
// 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
result.setText(userInput.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();
}
});
}
}
答案 1 :(得分:2)
希望这会对你有所帮助
AlertDialog.Builder alert = new AlertDialog.Builder(con);
final EditText input = new EditText(con);
String s="your text";
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(con, value, Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
答案 2 :(得分:1)
我会用尽可能少的代码带你完成这些步骤,因为我认为这会帮助你学到更多(比这里的其他答案):
final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom_box);
如果您需要,也可以链接到相当不错的tutorial,这里是Android documentation。
答案 3 :(得分:1)
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private TextView result;
private TextView result2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// components from main.xml
button = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.textView1);
result2 = (TextView) findViewById(R.id.textView2);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View view= li.inflate(R.layout.dailog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(view);
final EditText userInput = (EditText) view
.findViewById(R.id.edit1);
final EditText userInput2 = (EditText) view
.findViewById(R.id.edit2);
// 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
result.setText(userInput.getText());
result2.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();
}
});
}
}
这是我的确切答案
答案 4 :(得分:0)
here是解释如何在Android中创建自定义对话框的链接,这将有助于您
答案 5 :(得分:0)
Call ForgetPswPopUp() method name
public void ForgetPswPopUp() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
HomeActivity.this);
alertDialog.setTitle("set Title");
final EditText input = new EditText(HomeActivity.this);
input.setHint("Textbox hint");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, position);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setNegativeButton("Button name",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}