• 一个Activity中使用两个layout实例


    package com.sbs.aas2l;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class aas2l extends Activity implements OnClickListener {
        /** Called when the activity is first created. */
    private String[] data= {"Item-1", "Item-2", "Item-3"};
    private Button btn, btn2;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);
            set_aas2l_layout();
        }
        public void set_aas2l_layout() {
         setContentView(R.layout.aas2l);
         btn = (Button)findViewById(R.id.pu_btn);
         btn.setOnClickListener(this);
         btn2 = (Button)findViewById(R.id.exit_btn);
         btn2.setOnClickListener(this); 
        }
        public void onClick(View v){
         if (v == btn) this.set_pu_layout();
         if(v == btn2) this.finish(); }
         public void set_pu_layout(){
         setContentView(R.layout.pickup);
         ListView lv = (ListView)findViewById(R.id.list);
         ArrayAdapter<String> arrayAdapter
         = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
         lv.setAdapter(arrayAdapter);
         lv.setOnItemClickListener(listener);
        }
        OnItemClickListener listener= new OnItemClickListener(){
         public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3){
          set_aas2l_layout();
          TextView tv = (TextView)findViewById(R.id.tv);
          tv.setText("select: " + data[arg2]);
         }
        };
    }
    step3。编写ass2l.xml (当然也可以在main.xml中写,这次我新建一个。)
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    
    <TextView android:id= "@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/dialog" 
    />
    <Button android:id= "@+id/pu_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/pickup" 
    />
    <Button android:id= "@+id/exit_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/exit" 
    />
    </LinearLayout>
    
    step4。编写pickup.xml(点击select按钮后显示的布局,是一个listview。)
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, aas2l" 
    />
    <ListView android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />
    </LinearLayout>
    
    step5。修改strings.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, aas2l!</string>
        <string name="app_name">adroid activity surport 2 layouts</string>
        <string name="pickup">select please</string>
        <string name="exit">Exit</string>
        <string name="dialog">dialog</string>
    </resources>
    
    step6。运行。
    
    如图:
    
    初始界面
    
    点击select后
    
    选择后
    
    
    
    end 。。。。
    step3。编写ass2l.xml (当然也可以在main.xml中写,这次我新建一个。)
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    
    <TextView android:id= "@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/dialog" 
    />
    <Button android:id= "@+id/pu_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/pickup" 
    />
    <Button android:id= "@+id/exit_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dip"
    android:text="@string/exit" 
    />
    </LinearLayout>
    
    step4。编写pickup.xml(点击select按钮后显示的布局,是一个listview。)
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, aas2l" 
    />
    <ListView android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    />
    </LinearLayout>
    
    step5。修改strings.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="hello">Hello World, aas2l!</string>
        <string name="app_name">adroid activity surport 2 layouts</string>
        <string name="pickup">select please</string>
        <string name="exit">Exit</string>
        <string name="dialog">dialog</string>
    </resources>
    
    step6。运行。
    
    如图:
    
    初始界面
    
    点击select后
    
    选择后
    
    
    
    end 。。。。
  • 相关阅读:
    Java读写.properties文件实例,解决中文乱码问题
    web项目的getContextPath()
    PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
    Eclipse安装与配置
    Linux基础整理 + 注释
    git命令集合
    遍历List集合的三种方法
    使用jqueryUI和corethink实现的类似百度的搜索提示
    corethink功能模块探索开发(十八)前台页面插入jit前端数据可视化库
    corethink功能模块探索开发(十七)opencmf.php 配置文件
  • 原文地址:https://www.cnblogs.com/niray/p/3815121.html
Copyright © 2020-2023  润新知