[英]popup error message for android app
我是android應用程序開發的新手,這是我的第一個應用程序。 我正在嘗試從用戶那里獲取一些數據,然后將其發送到我的php后端Web服務。
但是,我想啟用異常處理並在發生某種錯誤時出現錯誤彈出消息。
我的signup.java文件中目前有以下代碼
//When the send button is clicked
public void send(View v)
{
try{
// CALL Validation method to make post method call
validation();
}
catch(Exception ex)
{
// Error popup message to go with the error.
}
}
我的signup.xml表單如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/signup_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Please Register To Get Started"
android:textSize="20sp" />
.
.
.
<Button
android:id="@+id/signupbutton"
style="@android:style/Widget.Material.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/phone"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@drawable/rg_bg"
android:text="Signup"
android:textColor="#ffffff"
android:textSize="20sp"
android:onClick="send" />
</RelativeLayout>
我需要對sigupform.xml文件和signup.java文件的相應代碼添加哪些修改或元素,以實現所需的解決方案?
使用android AlertBuilder
類的簡單彈出對話框
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
代碼源鏈接
要么
Toast toast = Toast.makeText(context, text, duration);
toast.show();
這是我過去的工作方式:
public class Tools {
public static void exceptionToast(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
當我感到興奮時:
Tools.exceptionToast(getApplicationContext(), e.getMessage());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.