2017-03-17 73 views
0

我目前正在開發一個應用程序,它可以讓用戶註冊並輸入他/她的詳細信息。我想顯示「成功註冊!」提示或消息後,用戶只需按下提交按鈕,以便用戶知道他/她輸入的詳細信息已提交。按鈕上的提示按

這裏是我的提交按鈕:

@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; 
         } 
     } 
    ); 
+0

使用'Toast'或'AlertDialog' –

回答

3

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(); 
    } 
1

試試這個:

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(); 
+1

提示:你可以鏈的......例如'新生成器()setCancelable(假).setTitle()顯示()' –

+0

您好,感謝。響應。我正在嘗試這一個。不過,我對「Intent intent = new Intent ....」一行很困惑。註冊發生的活動的名稱是RegisterActivity。那麼,我可以將其替換爲SignupActivity或TargetActivity?此外,我越來越無法解決符號錯誤。 – AndyMarty

+0

嗨AndyMarty,對於遲到的回覆感到抱歉,意圖用於在Android中的兩個活動之間導航。如果您想要將用戶從當前活動移到另一個活動,您需要使用該意圖。正如你所說你現在的活動是RegisterActivity,你需要這樣做Intent intent = new Intent(RegisterActivity.this,TargetActivity.class);這裏的TargetActivity.class是你需要移動用戶的活動。它的願望,它不是強制性的使用彈出消息的意圖,你可以把你的代碼置於意圖的地方。 –

1

你可以這樣做多種方式,

  1. 警告對話框
  2. 吐司
  3. 彈出的對話框

讓我們用警報對話框來做。

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(); 
+0

爲什麼不在'Builder'上設置''東西? –

+0

沒有讓你先生! – Radhey

+0

查看其他答案和我的評論http://stackoverflow.com/a/42849467/2308683 –