Android之输入用户名和密码验证

今天做的就是在界面输入用户名和密码,如果正确的话显示登陆成功,否则就是失败了!

界面很挫,就是很挫!

输入用户名:Android

密码:2014

结果如下:

接着重置,返回原来图片!

如果密码错误呢?

点重置返回原来界面!

接着长按图标按钮,就出现动态textview!

具体代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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"
    tools:context="com.example.practice2.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:gravity="center" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:gravity="center"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center"
        android:text="您好,请输入用户名和密码" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.53"
            android:text="重置" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.47"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="" />

</LinearLayout>

后代:

package com.example.practice2;



import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
	
	EditText account = null;
	EditText code = null;
	ImageButton ibtn = null;
	Button btn = null;
	TextView information = null;
	TextView mcont = null;
	Context cont = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		
		
		
		
		account = (EditText) findViewById(R.id.editText1);
		code = (EditText) findViewById(R.id.editText2);
		ibtn = (ImageButton) findViewById(R.id.imageButton1);
		btn = (Button) findViewById(R.id.button1);
		information = (TextView) findViewById(R.id.textView1);
		mcont =(TextView) findViewById(R.id.textView2);
		cont = this;
		mcont = new TextView(cont);
        mcont.setText("万万没想到");
		//ibtn = new ImageButton(this);
        
		ibtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String username = ((EditText)findViewById(R.id.editText1)).getText().toString();
        		String pwd = ((EditText)findViewById(R.id.editText2)).getText().toString();
        		if(username.equals("android")&&pwd.equals("2014")){
        			ibtn.setImageDrawable(getResources().getDrawable(R.drawable.state1));
        			account.setVisibility(View.GONE);
        			code.setVisibility(View.GONE);	
        			information.setText("用户名密码正确");
        		}
        		else{
        			ibtn.setImageDrawable(getResources().getDrawable(R.drawable.state2));
        			account.setVisibility(View.GONE);
        			code.setVisibility(View.GONE);	
        			information.setText("用户名密码错误");
        		}
			}	
		});
		
		ibtn.setOnLongClickListener(new OnLongClickListener(){
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				LinearLayout mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout1);
    			mcont = new TextView(cont);
    			mcont.setText("万万没想到");
    			mLinearLayout.addView(mcont);
				//mLinearLayout.setLayoutParams(new RelativeLayout.LayoutParams(-1, -1));

				//TextView mView = new TextView(this);
				//mView.setId(2);
				//mView.setText("this is a test text!");
				//RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				//    -2, -2);
				// layoutParams.setMargins(100, 100, 100, 100);
				//layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
				
				
				
				//Toast.makeText(cont, "万万没想到", Toast.LENGTH_LONG).show();
				
    			//mLinearLayout.addView(mcont);
				return true;
			}
			
		});
		
		btn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ibtn.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
				information.setText("您好,请输入用户名和密码");
    			account.setVisibility(0);
    			code.setVisibility(0);
    			account.setHint("username");
    			code.setHint("password");
    			account.setText("");
    			code.setText("");				
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}
}


发布了596 篇原创文章 · 获赞 603 · 访问量 128万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览