本人虽然学过一点安卓技术 但是怎奈 没有系统的学习过 这些东西 然后看了多方的资料 决定来学习一下 这些东西
然后因为我一开始 来学习安卓的话 是因为安卓逆向 所以我先做了一个关于登陆的程序
代码如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.lenovo.helloworld.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText"
android:hint="用户名"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:text="登陆"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:id="@+id/button" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_marginTop="37dp"
android:id="@+id/editText2"
android:hint="密码"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
package com.example.lenovo.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText UserName;
private EditText PassWord;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
UserName =(EditText) findViewById(R.id.editText);
PassWord = (EditText) findViewById(R.id.editText2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean X=check(UserName.getText().toString().trim(), PassWord.getText().toString().trim());
if(X==Boolean.TRUE)
{
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
});
}
public Boolean check(String name, String pass) //自定义函数check 这里用来检查用户名和密码是否是hfdcxy和1234
{
if (name.equals("pipixia") && pass.equals("1234")) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();//弹框
return Boolean.TRUE;
} else
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();//弹框
return Boolean.FALSE;
}
}
然后 运行结果如下
如果登陆成功
登陆到一个新的页面
这里就成功了
然后下面就是讲 将我们的控件 和我们的事件绑定起来
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
SetLister();
}
private void SetLister() {
OnClick onclick=new OnClick();
button1.setOnClickListener(onclick);
button2.setOnClickListener(onclick);
button3.setOnClickListener(onclick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
Toast.makeText(MainActivity.this, "按钮一", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this, "按钮二", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(MainActivity.this, "按钮三", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
这样 和 内部类效果是一样的 点一下 就是就会有一个弹框~~~~