按下按钮提示
Prompt on Button Press
我目前正在开发一款允许用户注册并输入 his/her 详细信息的应用程序。我想在用户按下提交按钮后显示 "Successfully Registered!" 提示或消息,以便用户知道输入的详细信息 he/she 已提交。
这是我的提交按钮:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Intent recvdIntent = getIntent();
mUsername = recvdIntent.getStringExtra("USERNAME");
mUsername = recvdIntent.getStringExtra("PASSWORD");
Button btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
submitUserData();
return;
}
}
);
AlertDialog
public void showAlertDialog(String title,String msg){
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this)
.setTitle(title) // Setting Dialog Title
.setMessage(msg)// Setting Dialog Message
.setCancelable(false)
.create();
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
吐司.
public static void showToast(Context context,String msg){
Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
}
试试这个:
final AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
builder.setCancelable(false);
builder.setTitle("Success");
builder.setMessage("Successfully registered, Sign in now");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(SignupActivity.this, TargetActivity.class);
startActivity(intent);
finish();
}
});
builder.show();
您可以通过多种方式完成,
- 警报对话框
- 吐司
- 弹出对话框
让我们使用警报对话框来完成。
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
我目前正在开发一款允许用户注册并输入 his/her 详细信息的应用程序。我想在用户按下提交按钮后显示 "Successfully Registered!" 提示或消息,以便用户知道输入的详细信息 he/she 已提交。
这是我的提交按钮:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Intent recvdIntent = getIntent();
mUsername = recvdIntent.getStringExtra("USERNAME");
mUsername = recvdIntent.getStringExtra("PASSWORD");
Button btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
submitUserData();
return;
}
}
);
AlertDialog
public void showAlertDialog(String title,String msg){
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this)
.setTitle(title) // Setting Dialog Title
.setMessage(msg)// Setting Dialog Message
.setCancelable(false)
.create();
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
吐司.
public static void showToast(Context context,String msg){
Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
}
试试这个:
final AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
builder.setCancelable(false);
builder.setTitle("Success");
builder.setMessage("Successfully registered, Sign in now");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(SignupActivity.this, TargetActivity.class);
startActivity(intent);
finish();
}
});
builder.show();
您可以通过多种方式完成,
- 警报对话框
- 吐司
- 弹出对话框
让我们使用警报对话框来完成。
AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();