您的位置:首页 > 移动开发 > Android开发

Android仿新浪微博启动界面或登陆界面(1)

2016-11-21 10:07 691 查看

本文为大家分享了Android模仿新浪微博启动界面&登陆界面的具体实现代码,供大家参考,具体内容如下

启动界面

主要有两个功能:

1.加载启动动画
2.判断网络,有者直接进入登陆界面,否则去设置网络

代码较简单,主要采用AlphaAnimation()方法和动画监听器,使一张图片产生渐变动画。在动画启动的时候判断网络,动画结束时完成判断并进入登陆界面。

/**
* Created by D&LL on 2016/5/25.
* 初始页面加载界面
*/
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ImageView splashimage = (ImageView) findViewById(R.id.splashimage);
//透明度渐变动画
AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
//设置动画时长
animation.setDuration(3000);
//将组件和动画进行关联
splashimage.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
//动画启动执行
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(SplashActivity.this, "欢迎使用DeMon制作微博", Toast.LENGTH_LONG).show();
//检查并网络的方法
Tools.checkNetWork(SplashActivity.this);
}
//动画结束执行
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
//动画重复执行
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}

使用ConnectivityManager获取系统的连接服务,然后获取代表联网状态的NetWorkInfo对象,获取网络的连接情况,如果没有网络则生成一个AlertDialog引导进行网络设置。该方法位于Tools.java中。

/**
* 设置网络
*
* @param context
*/
public static void checkNetWork(final SplashActivity context) {
if (!NetWorkStatus(context)) {
TextView msg = new TextView(context);
msg.setText("请设置网络!");
AlertDialog.Builder b = new AlertDialog.Builder(context).
setIcon(R.drawable.notnet)
.setTitle("没有可用的网络")
.setMessage("是否对网络进行设置?");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//跳转到设置网络界面
context.startActivity(new Intent(Settings.ACTION_SETTINGS));
}
}).setNeutralButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
context.finish();
}
}).create().show();
}
}
/**
* 判断网络状态
*/
public static boolean NetWorkStatus(Context context) {
//获取系统的连接服务
ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context
.CONNECTIVITY_SERVICE);
if (connect == null) {
return false;
} else {
// 获取代表联网状态的NetWorkInfo对象,获取网络的连接情况
NetworkInfo[] infos = connect.getAllNetworkInfo();
if (infos != null) {
for (NetworkInfo network : infos) {
if (network.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}

SplashActivity的布局文件splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/splashimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/splashimage"/>
<ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_marginBottom="64dp"
android:layout_marginStart="130dp">
</ProgressBar>
</RelativeLayout>

 

登陆界面

此界面只有几个按钮,故合在一条博客里。

微博按钮触发进入到到授权界面

public class LoginActivity extends Activity {
private Button sinalogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sinalogin = (Button) findViewById(R.id.sinalogin);
sinalogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, OAuthActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}
});
}
}


布局文件login.xml两个自定义样式的EditText,一个普通按钮(此处纯粹摆设)。只有微博登陆按钮有用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="40dip"
android:background="@drawable/bg_edittext"
android:ems="10"
android:inputType="textPersonName">
</EditText>
<EditText
android:id="@+id/passworld"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginTop="20dip"
android:background="@drawable/bg_edittext"
android:ems="10"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:text="登录"/>
<Button
android:id="@+id/sinalogin"
android:layout_width="200dp"
android:layout_height="38dp"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:background="@drawable/weibologin"/>
</LinearLayout>
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息