我正在尝试开发网络应用程序,在网络断开时显示对话框。我做了一切,但对话框内的文字没有居中。
package com.example.ayyappaboddupalli.networkchecker;
import android.app.Application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
/**
* Created by ayyappaboddupalli on 10/23/2017.
*/
public class ApplicationClass extends Application implements android.app.AlertDialog.OnDismissListener {
private static ApplicationClass mInstance;
private NetworkObserver networkObserver;
private NetworkDetector detector;
private AlertDialog.Builder ad = null;
private AlertDialog alertDialog;
public AlertDialog getAlertDialog() {
return alertDialog;
}
@Override
public void onCreate() {
super.onCreate();
networkObserver = new NetworkObserver();
}
public static ApplicationClass getmInstance() {
if (mInstance == null) {
synchronized (Object.class) {
mInstance = mInstance == null ? new ApplicationClass() : mInstance;
}
}
return mInstance;
}
public NetworkObserver getNetworkObserver() {
if (networkObserver == null) {
networkObserver = new NetworkObserver();
return networkObserver;
} else {
return networkObserver;
}
}
public void registerReceiver(Context context) {
detector = new NetworkDetector();
context.registerReceiver(detector, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public void unRegisterReciver(Context context) {
try {
unregisterReceiver(detector);
} catch (Exception f) {
}
}
public void showDialog(Context context) {
if(alertDialog!=null)
{
alertDialog.dismiss();
}
ad = new AlertDialog.Builder(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.weak_net_dialog, null);
ad.setView(view);
alertDialog = ad.create();
alertDialog.show();
alertDialog.setCancelable(false);
}
@Override
public void onDismiss(DialogInterface dialog) {
alertDialog=null;
}
}
public class NetworkDetector extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean isNetOn=isnetworkOnline(context);
ApplicationClass.getmInstance().getNetworkObserver().setValue(isNetOn);
}
public boolean isnetworkOnline(Context context)
{
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in airplane mode it will be null
return (netInfo != null && netInfo.isConnected());
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
}
}
public class NetworkObserver extends Observable {
private boolean netWorkOff=false;
public boolean isNetWorkOff() {
return netWorkOff;
}
public void setValue(boolean netWorkOff) {
this.netWorkOff = netWorkOff;
setChanged();
notifyObservers();
}
}
@Override
public void update(Observable o, Object arg) {
if(ApplicationClass.getmInstance().getNetworkObserver().isNetWorkOff()) {
Toast.makeText(getApplicationContext(),"Network is online",Toast.LENGTH_LONG).show();
if(ApplicationClass.getmInstance().getAlertDialog()!=null&&ApplicationClass.getmInstance().getAlertDialog().isShowing())
ApplicationClass.getmInstance().getAlertDialog().dismiss();
}
else
{
ApplicationClass.getmInstance().showDialog(this);
// Toast.makeText(getApplicationContext(),"Network is offline",Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
在ur xml文件中使用以下行:
android:center_vertical = true
android:center_horizontal=true
如果您使用relativeLayout,它将起作用 还将宽度设置为对话的match_parent和editText
答案 1 :(得分:0)
试试这个:
${postBody}