如何有eclipse的安卓做一个简单的密码登录

如何有eclipse的安卓做一个简单的密码登录

材料:Eclipse和其的avd

来看一下展示效果

在这里插入图片描述
密码登录后进入下一个界面。
在这里插入图片描述

##下面就让我们开始吧

首先我们先了解一下什么是JAVA中怎么进行输入两个字符串进行匹配

如何插入一段漂亮的代码片

//简单的密码登录
public class tes1 {

	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		String username ;
			do{
				System.out.println("请输入用户名:");
				String name =input.next();
				System.out.println("请输入密码:");
				String pw1 = input.next();
				if(name.equals("zzp")&&pw1.equals("123"))
				System.out.println("恭喜"+name+"登录成功!");
				else
			    System.out.println("抱歉!"+name+"登录失败");
			}while(true);
		
	}
}

在这里插入图片描述
我们可以用equals进行匹配字符串,来达到是否输入密码正确的效果

接下来一些安卓小知识的认识

  1. 布局(activity_main.xml)

    <LinearLayout android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
       <LinearLayout android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1">
      <LinearLayout android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
       android:gravity="center">
       
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录界面"
        android:textColor="#436EEE"
        android:textSize="30sp" />
        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="输入用户名" >
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="80"
        android:hint="输入密码"
        android:inputType="textPassword" />
     </LinearLayout>
    <LinearLayout android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
       android:gravity="top">
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#436EEE"
        android:text="登录"
        android:textColor="#fff" />
   <com.example.zzptes.MarqueeText
            android:text="@string/test1"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true" />
  </LinearLayout>
   </LinearLayout>
      </LinearLayout>

// 布局控件解释

控件名解释
LinearLayout线性布局,可将屏幕分为水平和垂直方向
TextView文本控件,可在屏幕中显示文本
EditText可编辑文本框,可输入的文本
Button普通按钮,设置可点击的按钮
com.example.zzptes.MarqueeText自制的跑马灯

注:跑马灯可以直接用TextView控件,在后面加上属性:
android:ellipsize=“marquee”
android:focusable=“true”
android:focusableInTouchMode=“true”
也可达到效果但只能在一个TextView控件上*
2. 主页代码(MainActivity.java)

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        final EditText ET1=(EditText) findViewById(R.id.editText1);
        final EditText ET2=(EditText) findViewById(R.id.editText2);
       Button lobutton = (Button) findViewById(R.id.button1);
       //为button设置监听器
       lobutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				String inputET = ET1.getText().toString();
				String putET = ET2.getText().toString();
				if(inputET.equals("zzp") && putET.equals("123456"))
				{
					Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
					//使用Intent实现不同页面的传参
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setClass(MainActivity.this,NextActivity.class);
					intent.putExtra("stringname","恭喜zzp登录");
                    startActivity(intent);
				
				}
				else
				{
					Toast.makeText(MainActivity.this, "登陆失败!", Toast.LENGTH_SHORT).show();
					Toast.makeText(MainActivity.this, "你的帐号或密码有误!", Toast.LENGTH_SHORT).show();
				}
			}
		});
     
    }
        }

对于Intent的一些用法:
在发送端传值,构建Intent
Intent intent = new Intent();
向Intent中添加传递参数
inten.putExtra(“键名”,“键值”);
在接受端传递过来的值
Intent : Intent intent=getIntent();
取出Intent的参数
String value = intent.getExtra(“键”);
3. 另一个界面(NextActivity.java)

public class NextActivity extends Activity {
	private TextView lotextview;
	private ImageButton IB1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        IB1=(ImageButton) findViewById(R.id.imageButton1);
        IB1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Toast.makeText(NextActivity.this, "SEE YOU !", Toast.LENGTH_SHORT).show();
				Intent intent=new Intent();
				intent.setClass(NextActivity.this,MainActivity.class);
				NextActivity.this.startActivity(intent);
			}
        });
        Intent intent = getIntent();
        String value = intent.getStringExtra("stringname");
        lotextview = (TextView) findViewById(R.id.textView1);
        lotextview.setText(value);
        Toast.makeText(NextActivity.this, "新的内容Coming Soon!", Toast.LENGTH_SHORT).show();
    }
}

这里就简单设置Intent的获取接受数据就好
注:要在AndroidMainfile.xml中注册改页面,否则无法运行


完成之后就可以做出登录界面和密码登录效果了,我们可以在在这些基础加点东西使得这效果更完善,比如在布局里添加一些美工等等,发挥你的想象力试一试吧。
第一次做这博客,以后还会更加完善。谢谢大家!

  • 9
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值