android应用开发实践一:Activity与控件布局,事件监听之学习实践

布局文件采用相对布局:相对布局灵活,使用最频繁。常用的布局方式有:线性布局(水平或垂直),相对布局,绝对布局,网格布局,表格布局,列表布局,标签布局,其中用的最多是相对布局,其次为网格布局(图片浏览),标签布局(多页面切换)等。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >     
 
     <Button
      android:id="@+id/MyButon1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:textColor="#FF0000"
      android:textSize="25sp"
      
        />
      <Button
      android:id="@+id/MyButon2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_toRightOf="@id/MyButon1"
      android:textColor="#FF0000"
      android:textSize="25sp"
      />
     <Button
      android:id="@+id/MyButon3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/MyButon2"
     android:textColor="#FF0000"
      android:textSize="25sp"
      />
      <Button
      android:id="@+id/MyButon4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_toRightOf="@+id/MyButon3"
      android:textColor="#FF0000"
      android:textSize="25sp"
        
      />
        <Button
      android:id="@+id/MyButon5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_toRightOf="@+id/MyButon4"
      android:textColor="#FF0000"
      android:textSize="25sp"
        
      />
      <TextView
      android:id="@+id/MytextView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textColor="#008888"
      android:textSize="18sp"
      android:layout_below="@+id/MyButon1"
      android:layout_alignParentLeft="true"
       />
      
  </RelativeLayout >

字符变量为:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">天机测算</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="mytextview">hello world,The is my first Textview on 2014-5-3
    </string>
    <string name="Mybutton">button1</string>
</resources>

java源代码为:

package com.example.helloworld;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {

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

Button bt1=(Button)findViewById(R.id.MyButon1);
Button bt2=(Button)findViewById(R.id.MyButon2);
Button bt3=(Button)findViewById(R.id.MyButon3);
Button bt4=(Button)findViewById(R.id.MyButon4);
Button bt5=(Button)findViewById(R.id.MyButon5);
bt1.setText(" A ");
   bt2.setText(" B ");
bt3.setText(" C ");
   bt4.setText(" D ");
   bt5.setText(" E ");
   bt1.setOnClickListener(listener);
   bt2.setOnClickListener(listener);
   bt3.setOnClickListener(listener);
   bt4.setOnClickListener(listener);
   bt5.setOnClickListener(listener);
  TextView tv=(TextView)findViewById(R.id.MytextView);
  tv.setText("\t一语破天机,看看你今天的运气如何?");
}

private OnClickListener listener =new OnClickListener()
{
@Override
public void onClick(View v) {
Button btx=(Button)v;
TextView tv=(TextView)findViewById(R.id.MytextView);
switch(btx.getId())
{
case R.id.MyButon1:
Toast.makeText(MainActivity.this, "运气一般", Toast.LENGTH_LONG).show();
 tv.setText("\t恩,你今天运气一般,请踏实工作,真诚待人,戒骄戒躁,赚点人品吧!");
break;
case R.id.MyButon2:
Toast.makeText(MainActivity.this, "人品爆发", Toast.LENGTH_LONG).show();
tv.setText("\t哇塞,你今天运气超好,建议去买张彩票,一夜逆袭机会来了!");
break;
case R.id.MyButon3:
Toast.makeText(MainActivity.this, "运交华盖", Toast.LENGTH_LONG).show();
tv.setText("\t哦,你今天运交华盖,建议不要出门了,小心为妙!");
break;
case R.id.MyButon4:
Toast.makeText(MainActivity.this, "运气还不错", Toast.LENGTH_LONG).show();
tv.setText("\t嗨,今天走路注意看地面,前面会有人掉钱包,钱包没钱,只有一张公交卡!");
break;


case R.id.MyButon5:
Toast.makeText(MainActivity.this, "运气有点背", Toast.LENGTH_LONG).show();
tv.setText("\t晕,你今天心神不宁,可能会丢三落四,出门记得带好钥匙和钱包!");
break;
 
}

}

};



}

编译后产生的apk生成的图标为:


软件界面:


测试场景一:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江海细流

如该文章对你有帮助,请支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值