20170118 ImageButton和ImageView運用
Android 練習的成果,以下是程式碼!
這也許可以和鬧鐘結合...
這裡也有個關鍵元素,"隨機
(骰子*6 猜拳*3 以此類推 樂透*56)
int iComPlay = (int)(Math.random()*3 + 1);"
// ImageButton和ImageView運用(猜拳遊戲)
//20170118 劉彥廷
MainActivity .java
package com.example.user.myapplication34;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTxtResult;
private ImageView mImgViewComPlay;
private ImageButton mImgBtnScissors, mImgBtnStone, mImgBtnPaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgViewComPlay = (ImageView)findViewById(R.id.imgViewComPlay);
mTxtResult = (TextView)findViewById(R.id.txtResult);
mImgBtnScissors = (ImageButton)findViewById(R.id.imgBtnScissors);
mImgBtnStone = (ImageButton)findViewById(R.id.imgBtnStone);
mImgBtnPaper = (ImageButton)findViewById(R.id.imgBtnPaper);
mImgBtnScissors.setOnClickListener(imgBtnScissorsOnClick);
mImgBtnStone.setOnClickListener(imgBtnStoneOnClick);
mImgBtnPaper.setOnClickListener(imgBtnPaperOnClick);
}
private View.OnClickListener imgBtnScissorsOnClick = new View.OnClickListener(){
public void onClick(View v){
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors); //出剪刀
mTxtResult.setText(getString(R.string.player_lose));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText(getString(R.string.player_lose));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText(getString(R.string.player_win));
}
}
};
private View.OnClickListener imgBtnStoneOnClick = new View.OnClickListener() {
public void onClick(View v) {
// 決定電腦出拳.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors);
mTxtResult.setText( getString(R.string.player_win));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText( getString(R.string.player_draw));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText( getString(R.string.player_lose));
}
}
};
private View.OnClickListener imgBtnPaperOnClick = new View.OnClickListener() {
public void onClick(View v) {
// 決定電腦出拳.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors);
mTxtResult.setText(getString(R.string.player_lose));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText( getString(R.string.player_win));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText(getString(R.string.player_draw));
}
}
};
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="400dp"
android:layout_height="match_parent"
tools:context="com.example.user.myapplication34.MainActivity"
android:background="@android:color/darker_gray">
<TextView android:id="@+id/txtCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_com_play"
android:layout_below="@id/txtTitle"
android:layout_alignLeft="@id/txtTitle"
android:textSize="20sp"
android:layout_marginBottom="20dp" />
<TextView android:id="@+id/txtMyPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_my_play"
android:layout_below="@id/txtTitle"
android:layout_alignRight="@id/txtTitle"
android:textSize="20sp"
android:layout_marginBottom="20dp" />
<ImageButton android:id="@+id/imgBtnScissors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/scissors"
android:layout_below="@id/txtMyPlay"
android:layout_alignLeft="@id/txtMyPlay"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/scissors" />
<ImageView android:id="@+id/imgViewComPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imgBtnScissors"
android:layout_alignLeft="@id/txtCom" />
<ImageButton android:id="@+id/imgBtnStone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stone"
android:layout_below="@id/imgBtnScissors"
android:layout_alignLeft="@id/imgBtnScissors"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/stone" />
<ImageButton android:id="@+id/imgBtnPaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/paper"
android:layout_below="@id/imgBtnStone"
android:layout_alignLeft="@id/imgBtnStone"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/stone" />
<TextView android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="20sp"
android:textColor="#0000FF"
android:layout_marginTop="20dp"
android:layout_below="@+id/imgBtnPaper"
android:layout_toRightOf="@+id/imgViewComPlay"
android:layout_toEndOf="@+id/imgViewComPlay" />
<TextView android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_title"
android:textSize="40sp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">電腦猜拳遊戲(No34)</string>
<string name="action_settings">Settings</string>
<string name="prompt_com_play">電腦出拳:</string>
<string name="prompt_my_play">玩家出拳:</string>
<string name="play_scissors">剪刀</string>
<string name="play_stone">石頭</string>
<string name="play_paper">布</string>
<string name="player_win">恭喜,你贏了!</string>
<string name="player_lose">很可惜,你輸了!</string>
<string name="player_draw">雙方平手!</string>
<string name="prompt_title">和電腦猜拳</string>
<string name="result">判定輸贏:</string>
</resources>
這也許可以和鬧鐘結合...
這裡也有個關鍵元素,"隨機
(骰子*6 猜拳*3 以此類推 樂透*56)
int iComPlay = (int)(Math.random()*3 + 1);"
// ImageButton和ImageView運用(猜拳遊戲)
//20170118 劉彥廷
MainActivity .java
package com.example.user.myapplication34;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTxtResult;
private ImageView mImgViewComPlay;
private ImageButton mImgBtnScissors, mImgBtnStone, mImgBtnPaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImgViewComPlay = (ImageView)findViewById(R.id.imgViewComPlay);
mTxtResult = (TextView)findViewById(R.id.txtResult);
mImgBtnScissors = (ImageButton)findViewById(R.id.imgBtnScissors);
mImgBtnStone = (ImageButton)findViewById(R.id.imgBtnStone);
mImgBtnPaper = (ImageButton)findViewById(R.id.imgBtnPaper);
mImgBtnScissors.setOnClickListener(imgBtnScissorsOnClick);
mImgBtnStone.setOnClickListener(imgBtnStoneOnClick);
mImgBtnPaper.setOnClickListener(imgBtnPaperOnClick);
}
private View.OnClickListener imgBtnScissorsOnClick = new View.OnClickListener(){
public void onClick(View v){
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors); //出剪刀
mTxtResult.setText(getString(R.string.player_lose));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText(getString(R.string.player_lose));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText(getString(R.string.player_win));
}
}
};
private View.OnClickListener imgBtnStoneOnClick = new View.OnClickListener() {
public void onClick(View v) {
// 決定電腦出拳.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors);
mTxtResult.setText( getString(R.string.player_win));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText( getString(R.string.player_draw));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText( getString(R.string.player_lose));
}
}
};
private View.OnClickListener imgBtnPaperOnClick = new View.OnClickListener() {
public void onClick(View v) {
// 決定電腦出拳.
int iComPlay = (int)(Math.random()*3 + 1);
// 1 – 剪刀, 2 – 石頭, 3 – 布.
if (iComPlay == 1) {
mImgViewComPlay.setImageResource(R.drawable.scissors);
mTxtResult.setText(getString(R.string.player_lose));
}
else if (iComPlay == 2) {
mImgViewComPlay.setImageResource(R.drawable.stone);
mTxtResult.setText( getString(R.string.player_win));
}
else {
mImgViewComPlay.setImageResource(R.drawable.paper);
mTxtResult.setText(getString(R.string.player_draw));
}
}
};
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="400dp"
android:layout_height="match_parent"
tools:context="com.example.user.myapplication34.MainActivity"
android:background="@android:color/darker_gray">
<TextView android:id="@+id/txtCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_com_play"
android:layout_below="@id/txtTitle"
android:layout_alignLeft="@id/txtTitle"
android:textSize="20sp"
android:layout_marginBottom="20dp" />
<TextView android:id="@+id/txtMyPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_my_play"
android:layout_below="@id/txtTitle"
android:layout_alignRight="@id/txtTitle"
android:textSize="20sp"
android:layout_marginBottom="20dp" />
<ImageButton android:id="@+id/imgBtnScissors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/scissors"
android:layout_below="@id/txtMyPlay"
android:layout_alignLeft="@id/txtMyPlay"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/scissors" />
<ImageView android:id="@+id/imgViewComPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imgBtnScissors"
android:layout_alignLeft="@id/txtCom" />
<ImageButton android:id="@+id/imgBtnStone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stone"
android:layout_below="@id/imgBtnScissors"
android:layout_alignLeft="@id/imgBtnScissors"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/stone" />
<ImageButton android:id="@+id/imgBtnPaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/paper"
android:layout_below="@id/imgBtnStone"
android:layout_alignLeft="@id/imgBtnStone"
android:paddingLeft="15dp"
android:paddingRight="15dp"
app:srcCompat="@drawable/stone" />
<TextView android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="20sp"
android:textColor="#0000FF"
android:layout_marginTop="20dp"
android:layout_below="@+id/imgBtnPaper"
android:layout_toRightOf="@+id/imgViewComPlay"
android:layout_toEndOf="@+id/imgViewComPlay" />
<TextView android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prompt_title"
android:textSize="40sp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">電腦猜拳遊戲(No34)</string>
<string name="action_settings">Settings</string>
<string name="prompt_com_play">電腦出拳:</string>
<string name="prompt_my_play">玩家出拳:</string>
<string name="play_scissors">剪刀</string>
<string name="play_stone">石頭</string>
<string name="play_paper">布</string>
<string name="player_win">恭喜,你贏了!</string>
<string name="player_lose">很可惜,你輸了!</string>
<string name="player_draw">雙方平手!</string>
<string name="prompt_title">和電腦猜拳</string>
<string name="result">判定輸贏:</string>
</resources>
留言
張貼留言