The second day of android learning

Today's main task is to implement the drawing board function on the Android side. In order to enrich the content of the task, I plan to simulate the game "draw and guess" mode for development.

The first is the login interface, the same as the interface developed on the first day, fill in the account password, select the game server to log in, although I did not verify the account password here, but for the next UI can have a more realistic The game effect, so fill in all the information before logging in, the following is the login interface:



  

The login interface is mainly the entry of the account password and the activity jump of the login. I will not post the code here.

 

Next is the interface after logging in. This interface is where the player enters the answer and prompt information, just like "you draw and I guess". First, the computer issues the question and then asks one player to draw, and the other player follows the prompt information. Make a guess. The difference is that the answers and prompt information here must be manually entered by the player, and the following is the interface of the game

 



 

This interface is also relatively simple. The java code in the background is simply to locate each component and pass some values. No code is posted here.

 

Next is the key part of the whole game, which is the drawing module. The interface consists of these parts, the canvas, the prompt information, the color selector, the answer input box, the input history, the timer and the submit button. The main function is as follows. First, the player with the known answer draws according to the answer, and the other player guesses the answer within a given time according to the given prompt information and pattern. When the answer is correct, there will be a pop-up prompt. Otherwise, the answer will be put into the history of answering questions. When the time is up, the player has not guessed the answer, the system will also prompt and give the correct answer. The following is the beginning of the game.

 

The answer is entered incorrectly, the answer is included in the history record, please continue to answer!

 In order to have a better gaming experience, I added the nickname of the current account in front of the answer, is it better! That's why you need to enter an account number

 

 

If the time ends and the answer cannot be guessed, a pop-up box will remind you, press the confirm button to return to the interface for entering the answer

 



 

Click the OK button to return to the interface for entering the answer



 

 

When you enter the correct answer, a pop-up box will remind you, click OK to return to the interface for entering the answer

 

 



 

 

The following is the xml of the painting interface, all of which are some standard components

<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"
    tools:context=".Play" >

    <com.example.huaban.paintview
        android:id="@+id/paintview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/paintview"
        android:layout_alignTop="@+id/paintview"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="42dp"
       />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/paintview"
        android:text="@string/tijiao" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_alignLeft="@+id/paintview"
        android:layout_marginBottom="17dp"
     />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="18dp"
        android:ems="10" />

    <Spinner
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/paintview"
        android:layout_alignParentLeft="true"
        android:entries="@array/color"
         />

</RelativeLayout>

   The code implemented by java has comments in it, which will not be explained here.

 

package com.example.huaban;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Play extends Activity {
      private TextView tishi_view;
      private String daan;
      private EditText shuru;
      private TextView ed;
      private Button button;
      private Spinner spinner;
      private String color1;
      
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView(R.layout.activity_play);
		
		//Create a new Intent to accept the value passed from the previous Activity
		Intent intent=getIntent();
		daan=intent.getStringExtra("question");
		
		//Locate components, read and write
		tishi_view=(TextView)findViewById(R.id.textView1);
		shuru=(EditText)findViewById(R.id.editText1);
		ed=(TextView)findViewById(R.id.textView2);
		button=(Button)findViewById(R.id.button1);
		spinner=(Spinner)findViewById(R.id.color);
		
		
		//implementation of timer
		CountDownTimer cdt = new CountDownTimer(100000, 100) {  
		    @Override  
		    public void onTick(long millisUntilFinished) {  
		    	Intent intent=getIntent();
		    	//Display the countdown on the interface
		    	tishi_view.setText("提示:"+intent.getStringExtra("tishi")+"  "+"倒计时:"+millisUntilFinished + "");  
		    }  
		    @Override  
		    public void onFinish() {  
		    	//The popup box after the timer expires
		    	AlertDialog.Builder builder  = new Builder(Play.this);
				 builder.setTitle("Time is up");
				 builder.setMessage("Correct answer: "+ daan);
				 builder.setPositiveButton("是" , new DialogInterface.OnClickListener() {
			            @Override
			            public void onClick(DialogInterface dialog, int which) {
			            	Intent intent=new Intent();
			        		intent.setClass(Play.this, Question.class);
			        		Play.this.startActivity(intent);
			            }});
				 builder.show();
		    }  
		};
		//start the timer
		cdt.start();
		//tishi_view.setText("提示:"+intent.getStringExtra("tishi"));
		//Position of the color picker
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
			  
		    @Override  
		    public void onItemSelected(AdapterView<?> parent, View view,  
		            int position, long id) {  
		          
		        // get the value of the selected item  
		        color1 = (String) spinner.getSelectedItem();  
		        if(color1.equals("红色"))
					paintview.color1=-1001;
				
				else if(color1.equals("黄色"))
					paintview.color1=-1003;
				else if(color1.equals("绿色"))
					paintview.color1=-1004;
			
				else if(color1.equals("蓝色"))
					paintview.color1=-1006;
				else if(color1.equals("紫色"))
					paintview.color1=-1007;
			
				else
				paintview.color1=1000;
			
		        }
		    @Override  
		    public void onNothingSelected(AdapterView<?> parent) {  
		        // TODO Auto-generated method stub  
		          
		    }  });
		//Prompt to submit correct answer
		button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v)
			{Intent intent=getIntent();
				ed.setText(ed.getText().toString()+"\n"+intent.getStringExtra("zhanghu")+":"+shuru.getText().toString());
				if(daan.equals(shuru.getText().toString())){
					
				AlertDialog.Builder builder  = new Builder(Play.this);
				 builder.setTitle("恭喜" ) ;
				 builder.setMessage("The answer is correct");
				 builder.setPositiveButton("是" , new DialogInterface.OnClickListener() {
			            @Override
			            public void onClick(DialogInterface dialog, int which) {
			            	Intent intent=new Intent();
			        		intent.setClass(Play.this, Question.class);
			        		Play.this.startActivity(intent);
			            }});
				 builder.show();
				
				 }
			}
			
		});
		
	}

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

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326182167&siteId=291194637