Android studio 入门第二天《第一行代码》

一,创建和加载布局

app/src/main/res/new/directory,创建一个名为layout的目录,然后右击/layout resource file/

添加按钮代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_1"//在xml中定义一个id用+。如果是引用则无需用+
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Button_1"
        />
</LinearLayout>

firstactivity中在onCreate()中添加

public class firstactivity extends AppCompatActivity {
    @Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
}
}

在AndroidManifest文件中注册

需要配置主活动详情参考《第一行代码》,在<activity>中加入<intent-filter>标签

修改后源码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="activitytest.example.com.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".firstactivity"
            android:label="This is firstactivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在活动中使用toast

扫描二维码关注公众号,回复: 2877036 查看本文章

首先要定义个出发点就用先前的按钮在onCreate()方法添加代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);//获取布局文件中定义的元素,转化为一个BUtton对象
    button1.setOnClickListener(new View.OnClickListener(){//注册一个监听器
        @Override
        public void onClick(View v){
            Toast.makeText(firstactivity.this,"You clicked Button 1",
       
                    Toast.LENGTH_SHORT).show();//第一个参数就是Toast要求的上下文,第二个参数是现实的内容,第三个参数是显示的时长
        }


    });
}

使用menu

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/add_item"
        android:title="ADD"/>
    <item
        android:id="@+id/remove_item"//具体标签
        android:title="REMOVE"/>
</menu>

四销毁活动

五使用显隐intent

firstactivity代码(三,四,五)

package activitytest.example.com.activitytest;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class firstactivity extends AppCompatActivity {

    @Override



    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button) findViewById(R.id.button_1);//获取布局文件中定义的元素,转化为一个BUtton对象
        button1.setOnClickListener(new View.OnClickListener(){//注册一个监听器
            @Override
            public void onClick(View v){
                Toast.makeText(firstactivity.this,"You clicked Button 1",
              //  finish();//销毁活动
                        Toast.LENGTH_SHORT).show();//第一个参数就是Toast要求的上下文,第二个参数是现实的内容,第三个参数是显示的时长
                //Intent intent = new Intent(firstactivity.this,SecondActivity.class);//首先我们构建一个Intent,第一个作为上下文,第二幕作为目标活动(显式intent)
               // Intent intent= new Intent(Intent.ACTION_VIEW);
                //intent.addCategory("activitytest.example.com.activitytest.MY_CATEGORY");
                //intent.setData(Uri.parse("http://www.baidu.com"));
                //startActivity(intent);//在第一个的基础上打开第二个,执行这个intent
            }


        });
    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()){

            case R.id.add_item:

                Toast.makeText(this, "you clicked ADD", Toast.LENGTH_SHORT).show();

                break;

            case R.id.remove_item:

                Toast.makeText(this, "you clicked REMOVE", Toast.LENGTH_SHORT).show();

                break;

            default:

        }

        return true;
    }}

猜你喜欢

转载自blog.csdn.net/huzi99/article/details/81812634