Activity总结练习
package com.example.wang.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText et_usercode; EditText et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_password=(EditText)findViewById(R.id.et_password); et_usercode=(EditText)findViewById(R.id.et_usercode); } public void zhuce(View v) { Intent intent=new Intent(this,ZhuceActivity.class); startActivityForResult(intent,1); } String username; String usercode; String password; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==1) { if (resultCode==RESULT_OK) { username=data.getStringExtra("name"); usercode=data.getStringExtra("code"); password=data.getStringExtra("pass"); } } } public void denglu(View v) { String et_code=et_usercode.getText().toString(); String et_pass=et_password.getText().toString(); if (et_code.trim().length()==0||et_pass.trim().length()==0) { Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show(); return; } if (et_code==null||!et_code.equals(usercode)) { Toast.makeText(MainActivity.this, "用户未注册", Toast.LENGTH_SHORT).show(); return; } if (et_pass==null||!et_pass.equals(password)) { Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); return; } else { Toast.makeText(this,"用户登录成功",Toast.LENGTH_LONG).show(); //传递信息到TestActivity页面 Intent intent=new Intent(this,TestActivity.class); intent.putExtra("code",usercode); intent.putExtra("name",username); startActivity(intent); } } }
package com.example.wang.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ZhuceActivity extends AppCompatActivity { EditText usercode; EditText username; EditText password; Button bt_quxiao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhuce); usercode=(EditText)findViewById(R.id.usercode); username=(EditText)findViewById(R.id.username); password=(EditText)findViewById(R.id.password); bt_quxiao=(Button)findViewById(R.id.bt_quxiao); bt_quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setResult(RESULT_CANCELED,null); finish(); } }); } public void queding(View v) { String user_name=username.getText().toString(); if (user_name==null||user_name.trim().length()==0) { Toast.makeText(ZhuceActivity.this, "请正确输入用户名", Toast.LENGTH_SHORT).show(); return; } String user_code=usercode.getText().toString(); if (user_code==null||user_code.trim().length()==0) { Toast.makeText(ZhuceActivity.this, "请正确输入用户代码", Toast.LENGTH_SHORT).show(); return; } String pass_word=password.getText().toString(); if (pass_word==null||pass_word.trim().length()==0) { Toast.makeText(ZhuceActivity.this, "请正确填写用户密码", Toast.LENGTH_SHORT).show(); return; } Intent intent=new Intent(); intent.putExtra("name",user_name ); intent.putExtra("code",user_code); intent.putExtra("pass",pass_word); setResult(RESULT_OK,intent); finish(); } }
package com.example.wang.myapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class TestActivity extends AppCompatActivity { Button bt_1; EditText et_1; TextView tv_1; TextView tv_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); et_1=(EditText)findViewById(R.id.et_1); tv_1=(TextView)findViewById(R.id.tv_1); tv_2=(TextView)findViewById(R.id.tv_2); //从上一个页面传递信息到本页面 Intent intent=getIntent(); String str1=intent.getStringExtra("code"); String str2=intent.getStringExtra("name"); tv_1.setText("用户代码:"+str1); tv_2.setText("用户名称:"+str2); bt_1=(Button)findViewById(R.id.bt_1); //间接拨打电话 bt_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // //方法1 // String str1=et_1.getText().toString(); // // if (str1.trim().length()==0||str1==null) // { // Toast.makeText(TestActivity.this, "请正确填写号码", Toast.LENGTH_SHORT).show(); // return; // } // // Intent intent=new Intent(Intent.ACTION_DIAL); // // intent.setData(Uri.parse("tel:"+str1)); // // startActivity(intent); //方法2 调用getphone()方法 String phone=getphone(); if (phone==null) return; Intent intent=new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:"+phone)); startActivity(intent); } }); } //由于方法重复使用,定义一个方法 public String getphone() { String phone=et_1.getText().toString(); if (phone.trim().length()==0||phone==null) { Toast.makeText(TestActivity.this, "请正确填写号码", Toast.LENGTH_SHORT).show(); return null; } else { return phone; } } //直接拨打电话方法 public void zhibo(View v) { String phone=getphone(); if (phone==null) return; Intent intent=new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + phone)); try { startActivity(intent); } catch (Exception e) { e.printStackTrace(); } finish(); } }
<?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: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.wang.myapplication.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户代码" android:id="@+id/et_usercode"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:layout_below="@id/et_usercode" android:id="@+id/et_password"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/et_password"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="登陆" android:id="@+id/bt_denglu" android:onClick="denglu" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="注册" android:id="@+id/bt_zhuce" android:onClick="zhuce" android:layout_weight="1"/> </LinearLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <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:layout_margin="16dp" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户代码" android:id="@+id/usercode"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名称" android:id="@+id/username"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:id="@+id/password"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="取消" android:id="@+id/bt_quxiao" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="确定" android:id="@+id/bt_queding" android:layout_weight="1" android:onClick="queding"/> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <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="com.example.wang.myapplication.TestActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/tv_1" /> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/tv_2" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="170dp" android:layout_height="wrap_content" android:hint="电话号码" android:inputType="phone" android:id="@+id/et_1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="间接拨打" android:id="@+id/bt_1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="直接拨打" android:id="@+id/bt_2" android:onClick="zhibo"/> </LinearLayout> </LinearLayout>
\