Android_zk1所有功能

         * 1.include引用
* 2.selector选择器
* 3.toast判断不可为空
* 4.回显
* 5.修改
* 本技能实现以上5个功能


package com.example.bean;


import java.io.Serializable;


public class User implements Serializable {

private String name;
private String phone;
private String age;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String phone, String age) {
super();
this.name = name;
this.phone = phone;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", phone=" + phone + ", age=" + age + "]";
}

}


package com.example.day_05_002;


import java.io.Serializable;


import com.example.bean.User;


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


public class MainActivity extends Activity {


private TextView text_name;
private TextView text_phone;
private TextView text_age;
private Button btu_update;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
text_name = (TextView) findViewById(R.id.text_name);
text_phone = (TextView) findViewById(R.id.text_phone);
text_age = (TextView) findViewById(R.id.text_age);
btu_update = (Button) findViewById(R.id.btu_update);
//监听事件按钮
btu_update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//获取值
String name = text_name.getText().toString();
String phone = text_phone.getText().toString();
String age = text_age.getText().toString();
//意图跳转页面
Intent intent=new Intent(MainActivity.this, UpdateActivity.class);
//传值
User user = new User(name, phone, age);
intent.putExtra("user", user);
startActivityForResult(intent, 1);

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data==null) {
return;
}
if (requestCode==1&&resultCode==2) {
//赋值
User user = (User) data.getSerializableExtra("newuser");
text_name.setText(user.getName());
text_phone.setText(user.getPhone());
text_age.setText(user.getAge());
}
}

}


package com.example.day_05_002;


import java.io.Serializable;
import java.security.interfaces.RSAKey;


import com.example.bean.User;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class UpdateActivity extends Activity {


private EditText edit_name;
private EditText edit_phone;
private EditText edit_age;
private Button btu_save;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//初始化控件
edit_name = (EditText) findViewById(R.id.edit_name);
edit_phone = (EditText) findViewById(R.id.edit_phone);
edit_age = (EditText) findViewById(R.id.edit_age);
btu_save = (Button) findViewById(R.id.btu_save);
//获取值
Intent intent = getIntent();
User user = (User) intent.getSerializableExtra("user");
//将获取的值放在输入框中
edit_name.setText(user.getName());
edit_phone.setText(user.getPhone());
edit_age.setText(user.getAge());
//监听事件按钮
btu_save.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//获取值
String newname = edit_name.getText().toString();
String newphone = edit_phone.getText().toString();
String newage = edit_age.getText().toString();
//吐司
if (TextUtils.isEmpty(newname)||TextUtils.isEmpty(newphone)||TextUtils.isEmpty(newage)) {
Toast.makeText(UpdateActivity.this, "不可为空", 0).show();
}else {
//把获取的值传回去,使用无参意图传值
Intent intent = getIntent();
User value=new User(newname, newphone, newage);
intent.putExtra("newuser", value);
setResult(2,intent);
finish();
}
}
});
}



}


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        
    <item android:state_pressed="true" android:color="#ffffff"/>
    <item android:state_pressed="false" android:color="#000000"/>
    

</selector>


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        
    <item android:state_pressed="true" android:drawable="@color/red" />
    <item android:state_pressed="false" android:drawable="@color/blue" />
    

</selector>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#000000</color>
    <color name="blue">#ffffff</color>

</resources>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity"
    android:orientation="vertical" >

<include layout="@layout/d_include"/>
    
<TextView
   android:id="@+id/text_name" 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textSize="28sp"
   android:text="张三"
   android:gravity="center"
   />

<TextView
   android:id="@+id/text_phone" 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textSize="28sp"
   android:text="13381036931"
   android:gravity="center"
   />

<TextView
   android:id="@+id/text_age" 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:textSize="28sp"
   android:text="24"
   android:gravity="center"
   />
<Button 
   android:id="@+id/btu_update" 
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="修改"
   android:textColor="@color/btu_colorselected"
   android:background="@drawable/btu1_colorselected"/>

</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".UpdateActivity"
    android:orientation="vertical" >

       
    <include layout="@layout/d_include"/>
    
    <EditText 
        android:id="@+id/edit_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="30sp"
        />
    <EditText 
        android:id="@+id/edit_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="30sp"
        />
    <EditText 
        android:id="@+id/edit_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="30sp"
        />
<Button 
   android:id="@+id/btu_save"
   android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textColor="@color/btu_colorselected"
   android:background="@drawable/btu1_colorselected"
   />
    

</LinearLayout>


<?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="wrap_content"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/yingyun"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#f0f0f0"
   android:textSize="50sp"
   android:text="个人中心"
   android:gravity="center"
   />


</LinearLayout>


猜你喜欢

转载自blog.csdn.net/QQ849410011/article/details/80294191