zoukankan      html  css  js  c++  java
  • Android学习笔记——button_activity

    工程的功能是实现在一个acticity上点击按钮,切换到另外一个activity

    以下代码为MainActivity.java中的代码

    package com.example.button_activity;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        private Button myButton = null; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myButton = (Button)findViewById(R.id.myButton);
            myButton.setOnClickListener(new MyButtonListener());  
        }
        
        class MyButtonListener implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, OtherActivity.class);
            MainActivity.this.startActivity(intent);
            }
            
            
        }
            
    }

    以下代码为OtherActivity.java中的代码

    package com.example.button_activity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class OtherActivity extends Activity{
    
        private TextView myTextView = null; 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_other);
            myTextView = (TextView)findViewById(R.id.myTextView);
            myTextView.setText(R.string.other);
        }
        
    
    }

    以下代码为activity_main.xml中的代码

    <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"
        tools:context="${relativePackage}.${activityClass}" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <Button
            android:id="@+id/myButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            
            />
    
    </RelativeLayout>

    以下代码为activity_other.xml中的代码

    <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"
        tools:context="${relativePackage}.${activityClass}" >
    
        <TextView
            android:id="@+id/myTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
    
    </RelativeLayout>

    以下代码为string.xml中的代码

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">button_activity</string>
        <string name="hello_world">Hello world!</string>
        <string name="other">OtherActivity</string>
    </resources>

    以下代码为AndroidManifest.xml中的代码 

    注意修改package的名称

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.button_activity"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".OtherActivity"
                android:label="@string/other" >
            </activity>     
        </application>
    
    </manifest>

    如果不能运行请kill-adb和start-adb并重新启动eclipse

  • 相关阅读:
    前端学习资源
    CSS样式属性单词之Left
    CSS 解决 a标签去掉下划线 text-decoration: none无效 的解决方案
    CSS二级菜单
    position属性absolute与relative 详解
    CSS行高——line-height
    VS code注释快捷键
    CSS padding margin border属性详解
    block(块级元素)和 inline(内联元素) 的区别
    css(float浮动和clear清除)
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/4576965.html
Copyright © 2011-2022 走看看