Android EditText框

时间:2012-04-03 04:38:56

标签: android android-widget

我是一名初学者。尝试与最多十名玩家一起创建Dreidel游戏。 我有一个在xml文件中创建的editText框,以及一个onClickListener事件。我还有一个旋转按钮的onClickListener事件。

我以前的代码允许我旋转按钮并在相应的文本框中显示事件, 但是当我添加新代码时,得分是editText中的显示器,而不是常规的textView框。 我会复制这两个代码。我的想法是让我在EditText框中输入一个玩家金额,并在textView框中显示该分数。

以下是在textView Box中成功显示分数的旧代码(除了旋转的字符外)     package com.dreidelproject;

      import java.util.Random;
      import android.app.Activity;
      import android.os.Bundle;
      import android.app.*;
      import android.widget.*;
      import android.view.*;


     public class MainActivity extends Activity {
            // Variables
     private TextView widget34, textView1;
 private Button widget32;
     private ImageView widget33;
     double playerScore = 15;
     double moneyPot = 0;

     /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         //GUI created from main.xml
         setContentView(R.layout.main);
         //sets variables to objects in xml file
             this.widget34 = (TextView) findViewById(R.id.widget34);
         this.textView1 = (TextView) findViewById(R.id.textView1);
         this.widget33 = (ImageView) findViewById(R.id.widget33);
         this.widget32 = (Button) findViewById(R.id.widget32);



          // OnClickListener Event for Spin Button
         this.widget32.setOnClickListener(new View.OnClickListener() {
             public void onClick(View target) {
                  Random numGen = new Random();
                     int rNumber = numGen.nextInt(4);
                 if (rNumber  == 0)
                 {   
                 widget34.setText("You get nothing");
                 widget33.setImageResource(R.drawable.gimel);
                 textView1.setText("" + playerScore);
                 }
                 else if (rNumber  == 1)
                 {   
                 widget34.setText("You get half!");
                 widget33.setImageResource(R.drawable.hey);
                 playerScore = (.5 * moneyPot) + (playerScore);
                 textView1.setText("" + playerScore);
                 }
                 else if (rNumber  == 2)
                 { 
                 widget34.setText("You get it all");
                 widget33.setImageResource(R.drawable.nun);
                 playerScore = moneyPot + playerScore;
                 textView1.setText("" + playerScore);
                 }
                 else if (rNumber  == 3)
                 {
                 widget34.setText("Chip in a coin");
                 widget33.setImageResource(R.drawable.hebreww);
                 moneyPot++;
                 playerScore--;
                 textView1.setText("" + playerScore);
             }  
             }
         });
     }}

下面是新代码,没有textView框工作,分数显示在editTextBox中。

package com.dreidelproject;

import java.util.Iterator;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.app.*;
import android.widget.*;
import android.view.*;

import java.util.Scanner;

public class MainActivity extends Activity {
// Variables
private TextView widget34, textView1, editText1;
private Button widget32;
private ImageView widget33;
double playerScore = 15;
double moneyPot = 0;
int playerCount = 1;
Iterator<String> playerAmountScan = new Scanner(System.in);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //GUI created from main.xml
    setContentView(R.layout.main);
    //sets variables to objects in xml file
    this.widget34 = (TextView) findViewById(R.id.widget34);
    this.textView1 = (TextView) findViewById(R.id.textView1);
    this.widget33 = (ImageView) findViewById(R.id.widget33);
    this.widget32 = (Button) findViewById(R.id.widget32);
    this.editText1 = (TextView)findViewById(R.id.editText1);


     // OnClickListener Event for Spin Button
    this.widget32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View target) {
             Random numGen = new Random();
                int rNumber = numGen.nextInt(4);
                playerCount++;
                while (playerCount == 11)
                    playerCount = 0;
            if (rNumber  == 0)
            {   
            widget34.setText("You get nothing");
            widget33.setImageResource(R.drawable.gimel);
            textView1.setText("" + playerScore);
            }
            else if (rNumber  == 1)
            {   
            widget34.setText("You get half!");
            widget33.setImageResource(R.drawable.hey);
            playerScore = (.5 * moneyPot) + (playerScore);
            textView1.setText("" + playerScore);
            }
            else if (rNumber  == 2)
            { 
            widget34.setText("You get it all");
            widget33.setImageResource(R.drawable.nun);
            playerScore = moneyPot + playerScore;
            textView1.setText("" + playerScore);
            }
            else if (rNumber  == 3)
            {
            widget34.setText("Chip in a coin");
            widget33.setImageResource(R.drawable.hebreww);
            moneyPot++;
            playerScore--;
            textView1.setText("" + playerScore);
        }  
        }
    });

    //  OnClickListener Event for Player Amount
    this.editText1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            {
            playerAmountScan.next();
            editText1.equals(playerAmountScan);
            editText1.setText((CharSequence) playerAmountScan);
            }
               }
    });
}
}

0 个答案:

没有答案