第二周安卓学习笔记 页面跳转

一个主界面
package com.sdibt.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {
private Button chan4;
private LinearLayout cityLayout;
private TextView citytTextView;
private final static int CITY = 1, WEATHER = 2,LOGIN=3;// 请求码
private MenuItem loginItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chan4 = (Button) this.findViewById(R.id.chan4);
cityLayout = (LinearLayout) this.findViewById(R.id.layout_SelectCity);
citytTextView = (TextView) this.findViewById(R.id.textViewSelectCity);
// 监听
chan4.setOnClickListener(this);
cityLayout.setOnClickListener(this);

}

@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);
    loginItem=menu.findItem(R.id.login);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.login://请求
Intent intent=new Intent(MainActivity.this, LoginActivity.class);
startActivityForResult(intent, LOGIN);
break;

default:
    break;
}
return super.onOptionsItemSelected(item);

}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.layout_SelectCity:
// 页面跳转
Intent intent = new Intent(MainActivity.this,
CityListActivity.class);
// startActivity(intent);
// 1、步骤1:返回数据
startActivityForResult(intent, CITY);

        break;
    case R.id.chan4:
        Intent intent4 = new Intent();
        intent4.setClass(MainActivity.this, ProductListActivity.class);
        startActivity(intent4);
        break;
    default:
        break;
    }
}

// 步骤二:统一处理返回来的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case CITY:
        // 解析数据
        System.out.println("--"+data.getStringExtra("city"));
        if (resultCode == RESULT_OK) {
            citytTextView.setText(data.getStringExtra("city"));
        } else {

        }
        break;
    case WEATHER:
        if (resultCode == RESULT_OK) {// 天气情况按钮

        } else if (resultCode == RESULT_CANCELED) {// 空气质量按钮

        }
        break;
    case LOGIN://接收用户名
        if (resultCode==RESULT_OK) {
            loginItem.setIcon(null);
            loginItem.setTitle(data.getStringExtra("user"));

        }
        break;
    default:
        break;
    }

}

}

一个登陆界面
import com.sdibt.util.Constant;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements View.OnClickListener {
private EditText editUser, editPwd;
private Button submit, reset;
private CheckBox recPwd, autoLogin;
private String userName, userPwd;
private SharedPreferences sp;// 存储xml数据
private Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);// 捆绑页面
    // 引用控件
    editUser = (EditText) this.findViewById(R.id.editUser);
    editPwd = (EditText) this.findViewById(R.id.editPwd);
    submit = (Button) this.findViewById(R.id.submit);
    reset = (Button) this.findViewById(R.id.reset);
    recPwd = (CheckBox) this.findViewById(R.id.recPwd);
    autoLogin = (CheckBox) this.findViewById(R.id.autoLogin);
    System.out.println("--" + submit + recPwd);
    // 读取sp数据
    sp = this.getSharedPreferences(Constant.SP_FILE1, MODE_PRIVATE);
    if (sp.getBoolean("isRecPwd", true)) {
        editUser.setText(sp.getString("userName", ""));
        editPwd.setText(sp.getString("userPwd", ""));
        recPwd.setChecked(true);
    }
    // 监听复选框

    recPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO

            if (arg1) {
                Toast.makeText(LoginActivity.this, arg0.getText() + "选中",
                        Toast.LENGTH_SHORT).show();
            } else {
                /*Toast.makeText(LoginActivity.this, arg0.getText() + "取消",
                        Toast.LENGTH_SHORT).show();*/
                editor=sp.edit();
                editor.putBoolean("isRecPwd", false);
                editor.putString("userName", null);
                editor.putString("userPwd", null);
                editor.commit();
            }
        }
    });

    // 方式一:单个点击事件处理
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(LoginActivity.this, "你点击了submit",
                    Toast.LENGTH_SHORT).show();
            Toast.makeText(LoginActivity.this, "你" + editUser.getText(),
                    Toast.LENGTH_SHORT).show();
        }
    });
    reset.setOnClickListener(new MyListener());// 设置监听
    // 方式三:多个类似控件监听
    submit.setOnClickListener(this);
    reset.setOnClickListener(this);
}

// 方式二:内部类写法
class MyListener implements View.OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Toast.makeText(LoginActivity.this, "你点击了reset", Toast.LENGTH_SHORT)
                .show();

    }

}

// 方式三:多个类似控件监听
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.submit:
        /*
         * Toast.makeText(LoginActivity.this, "点击submit",
         * Toast.LENGTH_SHORT) .show();
         */
        userName = editUser.getText().toString();
        userPwd = editPwd.getText().toString();
        if (userName.equals(Constant.USER_NAME)
                && userPwd.equals(Constant.USER_PASSWORD)) {// 登录成功
            // 往首页传数据
            Intent intent = new Intent();
            intent.putExtra("user", userName);
            setResult(RESULT_OK, intent);
            if (recPwd.isChecked()) {
                // 存储sharedpreference用户名密码
                sp = this.getSharedPreferences(Constant.SP_FILE1,
                        MODE_PRIVATE);
                editor = sp.edit();
                editor.putBoolean("isRecPwd", true);
                editor.putString("userName", userName);
                editor.putString("userPwd", userPwd);
                editor.commit();
            }

            finish();

        } else {
            Toast.makeText(LoginActivity.this, "用户名密码错误",
                    Toast.LENGTH_SHORT).show();
            editUser.setText(null);
            editPwd.setText(null);
        }
        break;
    case R.id.reset:
        editUser.setText(null);
        editPwd.setText(null);
        break;

    default:
        break;
    }
}

}


你可能感兴趣的:(第二周安卓学习笔记 页面跳转)