使用ADT实现Android三页面制作及跳转和内容显示

目录

一:创建项目和三个页面

1.创建项目

2.创建页面

二:第一个页面自动跳转至第二个页面

1.页面背景设置为图片

2.在Java代码中实现页面自动跳转

三:创建页面及实行点击跳转和传递内容

1.编写用户注册页面

 2.编辑下拉列表内容和获取页面内容并实现点击跳转

 四:接收上页面内容并进行显示

1.在第三个页面创建文本框用来接收内容

2.吐司和文本显示接收的内容

五:最终三个页面效果展示

 总结


一:创建项目和三个页面

1.创建项目

打开ADT左上角点击File-New-Android Application Project(创建项目)

项目名注意大小写规范

2.创建页面

在我们所建的项目下在src右键new-other-Android-AndroidActivity(创建页面)

注意命名规范

二:第一个页面自动跳转至第二个页面

1.页面背景设置为图片

打开res下的layout找到activity_main.xml文件

先把代码修改为线性布局后在线性布局内设置系统自带图片


//修改线性布局
<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:background="@drawable/ic_launcher"
    tools:context=".MainActivity" >
</LinearLayout>

android:background="@drawable/ic_launcher"

此代码插入的是系统自带的安卓图片

2.在Java代码中实现页面自动跳转

打开src下的包中的MainActivity.java文件中编写以下代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.WindowManager;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        隐藏状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        隐藏标题栏
        getActionBar().hide();
        setContentView(R.layout.activity_main);

//        1.定义一个子线程
        Thread thread=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
//              捕捉异常
				try {
//					3000代表3000毫秒也就是3秒
//                  在此页面停止3秒后跳转到下一页面
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				2.跳转到第二个页面
//                LiuActivity代表要跳转的页面名字
				Intent it=new Intent(getApplicationContext(),LiuActivity.class);
				startActivity(it);
			}
		});
//        启动子线程
        thread.start();
    }
  
 
 
}

三:创建页面及实行点击跳转和传递内容

1.编写用户注册页面

打开src下的包中创建一个新的页面LiuActivity.java文件

 此图片只需修改Activity Name中的Activity前面的字母就行

打开res下的layout找到activity_liu.xml文件中编写以下代码

编写代码如下:


<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:orientation="vertical"
    tools:context=".LiuActivity" >
 
     <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp"
       >
       <TextView 
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="用户注册"
           android:gravity="center"
           android:textColor="#fff"
           android:background="#373c3f"/>
   </LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp">
      <TextView 
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="账号:"
          android:gravity="center_vertical|right"/>
      <EditText 
          android:id="@+id/ed_import;"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="2"
          android:hint="请输入QQ号/手机号"
          /> 
   </LinearLayout>
     <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp">
      <TextView 
         
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="密码:"
          android:gravity="center_vertical|right"/>
      <EditText 
           android:id="@+id/ed_password"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="2"
          /> 
   </LinearLayout>
     <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp">
      <TextView 
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:text="班级:"
          android:gravity="center_vertical|right"/>
      <Spinner 
          android:id="@+id/sp_pulldown"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="3"/>
   </LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp">
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:text="性别"
           android:layout_weight="1"
           android:gravity="center_vertical|right"/>
       <RadioGroup 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="2"
           android:orientation="horizontal">
           <RadioButton 
               android:id="@+id/rd_w
               "
               android:layout_width="50dp"
               android:layout_height="match_parent"
               android:text="男"/>
           <RadioButton 
               android:id="@+id/rd_m"
               android:layout_width="50dp"
               android:layout_height="match_parent"
               android:text="女"/>
       </RadioGroup>
   </LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp">
       <TextView 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="爱好:"
           android:gravity="center_vertical|right"/>
       <LinearLayout 
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="2">
           <CheckBox 
               android:id="@+id/cb_Internet"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:text="上网"/>
            <CheckBox 
                android:id="@+id/cb_chat"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:text="聊天"/>
             <CheckBox 
                 android:id="@+id/cb_sleep"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:text="睡觉"/>
              <CheckBox 
                  android:id="@+id/cb_book"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:text="看书"/>
       </LinearLayout>
   </LinearLayout>
   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:gravity="center">
       <Button 
           android:id="@+id/btn_confirm"
           android:layout_width="100dp"
           android:layout_height="match_parent"
           android:text="确定"
          />
        <Button 
            android:id="@+id/btn_cancel"
           android:layout_width="100dp"
           android:layout_height="match_parent"
           android:text="取消"
           />
   </LinearLayout>
</LinearLayout>

ADT内视图效果如下:(视图好看程度可自己调节)

 2.编辑下拉列表内容和获取页面内容并实现点击跳转

在src下的包中LiuActivity.java文件编写以下代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
 
public class LiuActivity extends Activity implements OnClickListener{
 
	 // 1:定义所有控件
	EditText Edimpot,Edpass;
	RadioButton rbw,rbm;
	CheckBox CbInternet,Cbchat,Cbsleep,Cbbook;
	Button Btnconfirm,Btncancel;
	private Spinner Sppull;
	String []arr=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_liu);
		  //2:找到所有有id的控件
		   into();
		// 准备控件所要显示的数据
	       arr= new String []{"移动215","移动214","移动213","移动212","移动211"};
	    // 使用适配器来进行页面和数据的绑定
	        ArrayAdapter<String> stt=new ArrayAdapter<String>(getApplicationContext(),R.layout.eeee,arr);
	        Sppull.setAdapter(stt);

	        // 3:设置点击按钮的点击事件
	        Btnconfirm .setOnClickListener(this);
	        
	}
	 public void into(){
		 Edimpot=(EditText) findViewById(R.id.ed_import);
		 Edpass=(EditText) findViewById(R.id.ed_password);
		  rbw=(RadioButton) findViewById(R.id.rd_w);
		  rbm=(RadioButton) findViewById(R.id.rd_m);
		  CbInternet=(CheckBox) findViewById(R.id.cb_Internet);
		  Cbchat=(CheckBox) findViewById(R.id.cb_chat);
		  Cbsleep=(CheckBox) findViewById(R.id.cb_sleep);
		  Cbbook=(CheckBox) findViewById(R.id.cb_book);
		  Btnconfirm=(Button) findViewById(R.id.btn_confirm);
		  Btncancel=(Button) findViewById(R.id.btn_cancel);
		  Sppull=(Spinner) findViewById(R.id.sp_pulldown);
	  }
 
 //获取内容
public void onClick(View arg0) {
	// 4: 获取用户名输入框
	String name=Edimpot.getText().toString();
	  // 5:获取密码输入框内容
	String pass=Edpass.getText().toString();
	 // 6:获取性别单选框内容
	String sex="";
	if(rbw.isChecked()){
		sex=rbw.getText().toString();
	}
	if(rbm.isChecked()){
		sex=rbm.getText().toString();
	}
	String hobby="";
	// 7:获取爱好多选框内容
	if(CbInternet.isChecked()){
		hobby+=CbInternet.getText().toString();
	}
	if(Cbchat.isChecked()){
		hobby+=Cbchat.getText().toString();
	}
	if(Cbsleep.isChecked()){
		hobby+=Cbsleep.getText().toString();
	}
	if(Cbbook.isChecked()){
		hobby+=Cbbook.getText().toString();
	}
	int i=Sppull.getSelectedItemPosition();
 
	//7.2获取下拉列表框所选内容
	String spt=arr[i];
	// 8:传值跳转
	Intent is=new Intent(getApplicationContext(),LioActivity.class);
	// 9:传递数据
	is.putExtra("name", name);
	is.putExtra("mm", pass);
	is.putExtra("xing", sex);
	is.putExtra("ai", hobby);
	is.putExtra("spt", spt);
	startActivity(is);
}  
 
}

 四:接收上页面内容并进行显示

1.在第三个页面创建文本框用来接收内容

跟上面第三步创建步骤一样创建LioActivity.java页面

打开res下的layout找到activity_lio.xml文件中编写以下代码

<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"
    tools:context=".OkActivity" >
 
    <TextView
        android:id="@+id/tv_a"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
 
</LinearLayout>

2.吐司和文本显示接收的内容

在src下的包中的LioActivity.java内编写以下代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
 
public class LioActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lio);
		Intent it=getIntent();
		String name=it.getStringExtra("name");
		String pass=it.getStringExtra("mm");
		String sex=it.getStringExtra("xing");
		String hobby=it.getStringExtra("ai");
		String spt=it.getStringExtra("spt");
		String str=name+":"+pass+":"+sex+":"+hobby+":"+spt;
		Toast.makeText(getApplicationContext(), str, 1).show();
		TextView tvShow=(TextView) findViewById(R.id.tv_a);
		tvShow.setText(str);
	
	}
 
	
}





五:最终三个页面效果展示

1.3秒跳转页面

 2.用户注册页面

3.接收内容页面

 总结

以上便是三页面进行跳转和内容显示是代码了,第一次在此平台上发布博客,也许会有很多不足之处和错误,希望各位能给予指出,赐教,以后也会以博客进行记录,与各位共同进步。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值