android学习阶段性总结

    对于安卓的学习进行到了第三周,我终于利用现有的知识创建出了一个勉强可以用来玩的小游戏。

    这个游戏的名字叫lightup,其实并不是真正意义上的lightup。

    游戏简介:该游戏界面有4*6个小方块,点击其中一个方块时,该方块及其上下左右一共5个方块(边缘处改变4个,角处改变3个)会改变颜色,即黑的变白,白的变黑。初始都为白色,直到所有都被点成黑色即为通关。

 

具体内容不再赘述。

    实现这个程序所用的主要技术:

           1,基本的组件:Button , ImageButton,Dialog ,

           2,镶嵌布局,table布局。

           3,用循环和直接引用R.xml中的地址的方式定义每个ImageButton及其功能。(一直不知道按钮数组如何实现。。。)

主界面的.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/panel"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <ImageButton
        android:id="@+id/ib_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="104dp"
        android:src="@drawable/start" />


    <ImageButton
        android:id="@+id/ib_howtoplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ib_start"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:scaleType="centerCrop"
        android:src="@drawable/howtoplay" />


    <ImageButton
        android:id="@+id/ib_about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/ib_howtoplay"
        android:layout_below="@+id/ib_howtoplay"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:src="@drawable/about" />


    <ImageButton
        android:id="@+id/ib_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ib_about"
        android:layout_marginTop="15dp"
        android:layout_toLeftOf="@+id/ib_start"
        android:src="@drawable/back" />

</RelativeLayout>

其对应的java代码:

package com.example.lightup;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;


public class MainActivity extends Activity {


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

//ib_howtoplay
creat();
}


private void creat() {
// TODO 自动生成的方法存根
//ImageButton ib_howtoplay
ImageButton ib_howtoplay = (ImageButton)findViewById(R.id.ib_howtoplay);
ib_howtoplay.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//显示howtoplay对话框
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.howtoplay_title)
.setMessage(R.string.howtoplay_msg)
.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface d,int i){

}
})
.show();
}
});
//ImageButton ib_about
ImageButton ib_about = (ImageButton)findViewById(R.id.ib_about);
ib_about.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//创建对话框
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton(R.string.ok,new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface d,int i)
{

}
})
.show();
}
});
//ImageButton ib_start
ImageButton ib_start = (ImageButton)findViewById(R.id.ib_start);
ib_start.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent it = new Intent();
it.setClass(MainActivity.this,playframe.class);
startActivity(it);
finish();
}
});
ImageButton ib_back = (ImageButton)findViewById(R.id.ib_back);
ib_back.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

游戏界面的.xml代码

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/panel" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#666666"
        android:layout_x="16dp"
        android:layout_y="14dp"
        android:text="@string/push" />
    
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_x="15dp"
        android:layout_y="80dp">
        <TableRow >
            <ImageButton 
                android:id="@+id/ib11" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib12" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib13" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib14" android:src="@drawable/white"/>
        </TableRow>
        <TableRow >
            <ImageButton 
                android:id="@+id/ib21" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib22" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib23" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib24" android:src="@drawable/white"/>
        </TableRow>
        <TableRow >
            <ImageButton 
                android:id="@+id/ib31" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib32" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib33" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib34" android:src="@drawable/white"/>
        </TableRow>
        <TableRow >
            <ImageButton 
                android:id="@+id/ib41" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib42" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib43" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib44" android:src="@drawable/white"/>
        </TableRow>
        <TableRow >
            <ImageButton 
                android:id="@+id/ib51" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib52" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib53" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib54" android:src="@drawable/white"/>
        </TableRow>
        <TableRow >
            <ImageButton 
                android:id="@+id/ib61" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib62" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib63" android:src="@drawable/white"/>
            <ImageButton 
                android:id="@+id/ib64" android:src="@drawable/white"/>
        </TableRow>
    </TableLayout>






    <ImageButton
        android:id="@+id/ib_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="37dp"
        android:layout_y="452dp"
        android:src="@drawable/back" />


    <Button
        android:id="@+id/b_restart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="214dp"
        android:layout_y="20dp"
        android:text="@string/restart"
        android:textColor="#666666" />


</AbsoluteLayout>

其对应的java代码:

package com.example.lightup;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;


public class playframe extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playframe);

//ImageButton ib_back
ImageButton ib_back = (ImageButton)findViewById(R.id.ib_back);
ib_back.setOnClickListener(new OnClickListener()                   //定义返回按钮
{
public void onClick(View v)
{
Intent it = new Intent();
it.setClass(playframe.this , MainActivity.class);
startActivity(it);
finish();
}
});
Button b_restart = (Button)findViewById(R.id.b_restart);
b_restart.setOnClickListener(new OnClickListener()                  //重置图片按钮颜色
{
public void onClick(View v)
{
int p = 0x7f080007;
for(int i = 0 ; i < 24 ; i ++)
{
ImageButton ib = (ImageButton)findViewById(p+i);
ib.setImageDrawable((Drawable)getResources().getDrawable(R.drawable.white));
}
}
});
creatButton();                                                      //table中的游戏按钮
}


private void creatButton() {
// TODO 自动生成的方法存根
int p = 0x7f080007;
for(int i = 0 ; i < 24 ; i ++)
{
ImageButton ib = (ImageButton)findViewById(p);
ib.setImageDrawable((Drawable)getResources().getDrawable(R.drawable.white));
            ib.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
/*if(((ImageButton) v).getDrawable().getCurrent().getConstantState() == getResources().getDrawable(R.drawable.black).getConstantState())
((ImageButton) v).setImageDrawable((Drawable)getResources().getDrawable(R.drawable.white));
                     else
                    ((ImageButton) v).setImageDrawable((Drawable)getResources().getDrawable(R.drawable.black));*/
changeDrawable(v);
}
});
            p++;
}
    }


protected void changeDrawable(View v) {                    //改变按钮及其周围按钮的图片
// TODO 自动生成的方法存根
int p = 0x7f080007;
int s = v.getId();
int c = s - p;
int d;
change(s);
d = c + 1;
if(c % 4 == 3) ;
else change(d+p);
d = c - 1;
if(c < 0 || c % 4 == 0) ;
else change(d+p);
d = c - 4;
if(d < 0) ;
else change(d+p);
d = c + 4;
if(d > 23) ; 
else change(d+p);
if(win())
new AlertDialog.Builder(playframe.this)
   .setMessage(R.string.win)
   .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener()
   {
   public void onClick(DialogInterface d,int i)
   {

   }
   })
   .show();
}


private boolean win() {                                     //赢了
// TODO 自动生成的方法存根
int p = 0x7f080007;
for(int i = 0 ; i < 24 ; i ++)
{
ImageButton ib = (ImageButton)findViewById(p+i);
if(ib.getDrawable().getCurrent().getConstantState() == getResources().getDrawable(R.drawable.white).getConstantState())
return false;
}
return true;
}


private void change(int p) {                                        //改变p位置按钮的图片
// TODO 自动生成的方法存根
ImageButton ib = (ImageButton)findViewById(p);
if(ib.getDrawable().getCurrent().getConstantState() == getResources().getDrawable(R.drawable.black).getConstantState())
ib.setImageDrawable((Drawable)getResources().getDrawable(R.drawable.white));
else
        ib.setImageDrawable((Drawable)getResources().getDrawable(R.drawable.black));
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值