关于android应用程序的执行

关于android应用程序的执行,android,android-layout,Android,Android Layout,我是一个Andoid新手,尝试创建一个应用程序,其中将有一个登录页面和一个自定义列表页面 如何合并这两个活动?请看一下代码 登录页面 登录页面 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"

我是一个Andoid新手,尝试创建一个应用程序,其中将有一个登录页面和一个自定义列表页面

如何合并这两个活动?请看一下代码


登录页面 登录页面

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".RelativeLayoutAndroidExample" >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="false"
        android:text="LOGIN" 
        android:layout_marginTop="14dp"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        />

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/text1"
         android:layout_marginTop="20dp"
         android:text="Username :"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     <EditText
         android:id="@+id/editText1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_alignTop="@+id/textView1"
         android:layout_toRightOf="@+id/textView1" 
         />

     <TextView
         android:id="@+id/textView2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/textView1"
         android:layout_marginTop="20dp"

         android:text="Password :"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     <EditText
         android:id="@+id/editText2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_alignTop="@+id/textView2"
         android:layout_toRightOf="@+id/textView2"
         android:inputType="textPassword" 
         />

     <Button
         android:id="@+id/btnSubmit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="false"
         android:layout_below="@+id/editText2"
         android:layout_centerInParent="true"
         android:text="Submit" />

     <Button 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true" 
         android:text="SIGNUP"
            android:layout_centerHorizontal="true"/>
</RelativeLayout>

自定义列表 主活动文件

 package com.androidexample.customlistview;

    import java.util.ArrayList;

    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.Toast;
    import android.app.Activity;
    import android.content.res.Resources;


    public class CustomListViewAndroidExample extends Activity {

        ListView list;
        CustomAdapter adapter;
        public  CustomListViewAndroidExample CustomListView = null;
        public  ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_list_view_android_example);

            CustomListView = this;

            /******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/
            setListData();

            Resources res =getResources(); 
            list=(ListView)findViewById(R.id.list);

            /**************** Create Custom Adapter *********/
            adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res);
            list.setAdapter(adapter);

        }

        /****** Function to set data in ArrayList *************/
        public void setListData()
        {

            for (int i = 0; i < 11; i++) {

                final ListModel sched = new ListModel();

                  /******* Firstly take data in model object ******/
                   sched.setCompanyName("Company "+i);
                   sched.setImage("image"+i);
                   sched.setUrl("http:\\\\www."+i+".com");

                /******** Take Model Object in ArrayList **********/
                CustomListViewValuesArr.add(sched);
            }

        }

        public void onItemClick(int mPosition)
        {
            ListModel tempValues = (ListModel) CustomListViewValuesArr.get(mPosition);

            Toast.makeText(CustomListView, 
                    ""+tempValues.getCompanyName()+" \nImage:"+tempValues.getImage()+" \nUrl:"+tempValues.getUrl(), 
                    Toast.LENGTH_LONG)
            .show();
        }


    }
package com.androidexample.customlistview;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.app.Activity;
导入android.content.res.Resources;
公共类CustomListViewAndroidExample扩展活动{
列表视图列表;
自定义适配器;
公共CustomListViewAndroidExample CustomListView=null;
public ArrayList CustomListViewValuesArrayList=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u custom\u list\u view\u android\u示例);
CustomListView=这个;
/********在Arraylist(CustomListViewValuesArrayList)中获取一些数据***********/
setListData();
Resources res=getResources();
list=(ListView)findViewById(R.id.list);
/****************创建自定义适配器*********/
适配器=新的CustomAdapter(CustomListView、CustomListViewValuesR、res);
list.setAdapter(适配器);
}
/******函数设置ArrayList中的数据*************/
public void setListData()
{
对于(int i=0;i<11;i++){
最终ListModel sched=新ListModel();
/*******首先在模型对象中获取数据******/
附表setCompanyName(“公司”+i);
附表setImage(“图像”+i);
sched.setUrl(“http:\\\www.“+i+”.com”);
/********在ArrayList中获取模型对象**********/
CustomListViewValuesArr.add(附表);
}
}
公共空间链接(集成)
{
ListModel tempValues=(ListModel)CustomListViewValuesArr.get(mPosition);
Toast.makeText(CustomListView,
“+tempValues.getCompanyName()+”\n图像:“+tempValues.getImage()+”\nUrl:“+tempValues.getUrl(),
吐司长度(长)
.show();
}
}
自定义适配器

package com.androidexample.customlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
public class CustomAdapter extends BaseAdapter   implements OnClickListener {

    /*********** Declare Used Variables *********/
    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater=null;
    public Resources res;
    ListModel tempValues=null;
    int i=0;

    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {

        /********** Take passed values **********/
        activity = a;
        data=d;
        res = resLocal;

        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if(data.size()<=0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder to contain inflated xml file elements ***********/
    public static class ViewHolder{

        public TextView text;
        public TextView text1;
        public TextView textWide;
        public ImageView image;

    }

    /*********** Depends upon data size called for each row , Create each ListView row ***********/
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;

        if(convertView==null){ 

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vi = inflater.inflate(R.layout.tabitem, null); 

            /******** View Holder Object to contain tabitem.xml file elements ************/
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);
            holder.text1=(TextView)vi.findViewById(R.id.text1);
            holder.image=(ImageView)vi.findViewById(R.id.image);

           /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        }
        else  
            holder=(ViewHolder)vi.getTag();

        if(data.size()<=0)
        {
            holder.text.setText("No Data");

        }
        else
        {
            /***** Get each Model object from Arraylist ********/
            tempValues=null;
            tempValues = (ListModel) data.get(position);

            /************  Set Model values in Holder elements ***********/
             holder.text.setText(tempValues.getCompanyName());
             holder.text1.setText(tempValues.getUrl());
             holder.image.setImageResource(res.getIdentifier("com.androidexample.customlistview:drawable/"+tempValues.getImage(),null,null));

             /******** Set Item Click Listner for LayoutInflater for each row ***********/
             vi.setOnClickListener(new OnItemClickListener(position));
        }
        return vi;
    }

    @Override
    public void onClick(View v) {
            Log.v("CustomAdapter", "=====Row button clicked");
    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener  implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int position){
             mPosition = position;
        }

        @Override
        public void onClick(View arg0) {
            CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
            sct.onItemClick(mPosition);
        }               
    }   
}
package com.androidexample.customlistview;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.content.Context;
导入android.content.res.Resources;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.view.OnClickListener;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
/*********适配器类使用BaseAdapter扩展,并使用OnClickListener实现************/
公共类CustomAdapter扩展BaseAdapter实现OnClickListener{
/***********声明使用的变量*********/
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共资源;
ListModel tempValues=null;
int i=0;
/*************自定义适配器构造函数*****************/
公共CustomAdapter(活动a、ArrayList d、资源resLocal){
/**********接受传递的值**********/
活动=a;
数据=d;
res=resLocal;
/***********布局充气器调用外部xml布局()**********************/
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
/********传递的Arraylist大小是多少************/
public int getCount(){

如果(data.size()成功登录后,只需将
意图
传递给您的CustomListViewAndroidExample
活动
,它就可以为您完成工作

Intent intent = new Intent(RelativeLayoutAndroidExample.this,
CustomListViewAndroidExample.class);
startActivity(intent);
试试这个

package com.androidexample.relativelayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class RelativeLayoutAndroidExample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relative_layout_android_example);
    Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(/*User name and password are correct*/)
            {
                Intent intent = new Intent(RelativeLayoutAndroidExample.this,CustomListViewAndroidExample.class);
                startActivity(intent);
            }
        }
    });

 }
}

尝试直到成功,如果有任何问题,请告诉我们
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidexample.customlistview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidexample.customlistview.CustomListViewAndroidExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Intent intent = new Intent(RelativeLayoutAndroidExample.this,
CustomListViewAndroidExample.class);
startActivity(intent);
package com.androidexample.relativelayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class RelativeLayoutAndroidExample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relative_layout_android_example);
    Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(/*User name and password are correct*/)
            {
                Intent intent = new Intent(RelativeLayoutAndroidExample.this,CustomListViewAndroidExample.class);
                startActivity(intent);
            }
        }
    });

 }
}