-1

我是android新手,我想知道当我点击button1时如何制作,然后button2文本视图将显示1 2,,但是如果我点击button2然后button1它将显示2 1

这是我的代码,但现在当我点击 button1 然后 button2,或者如果我点击 button2 然后 button1 它显示相同,请帮助我thanx。

现在我使用 getText 和 setText 完成了它,它可以按我的意愿工作,但是如果它的正确与否条件不起作用请帮助!它有什么问题,我的方法正确吗?

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.wordgame2);

    bu1 = (Button) findViewById(R.id.bu1);
    bu2 = (Button) findViewById(R.id.bu2);
    bu3 = (Button) findViewById(R.id.bu3);
    bu4 = (Button) findViewById(R.id.bu4);
    bu5 = (Button) findViewById(R.id.bu5);


    t1 = (TextView) findViewById(R.id.t1);

    i1 = (ImageView) findViewById(R.id.i1);
    i2 = (ImageView) findViewById(R.id.i2);

    bu1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            t1.setText(t1.getText()+" "+bu1.getText()); 

        }

    });

    bu2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    t1.setText(t1.getText()+" "+bu2.getText()); 


                }

            });

    bu3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            t1.setText(t1.getText()+" "+bu3.getText()); 

        }

    });

    bu4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub


            t1.setText(t1.getText()+" "+bu4.getText()); 
        }

    });

    bu5.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        if(t1.getText().toString().equals("a b c d")) //this is not working
            {

                i1.setVisibility(View.VISIBLE);
                i2.setVisibility(View.INVISIBLE);
            }       

            else {
                i2.setVisibility(View.VISIBLE);
                i1.setVisibility(View.INVISIBLE);
            }
        }

    });


}

04-21 07:00:48.690: E/AndroidRuntime(23874): 致命异常: main 04-21 07:00:48.690: E/AndroidRuntime(23874): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. arabicalphabets.reham/com.arabicalphabets.reham.Wordgame2}:java.lang.NullPointerException 04-21 07:00:48.690:E/AndroidRuntime(23874):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 04 -21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app .ActivityThread.access$600(ActivityThread.java:127) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 04-21 07: 00:48.690: E/AndroidRuntime(23874): 在 android.os.Handler。dispatchMessage(Handler.java:99) 04-21 07:00:48.690: E/AndroidRuntime(23874): at android.os.Looper.loop(Looper.java:137) 04-21 07:00:48.690: E/ AndroidRuntime(23874): 在 android.app.ActivityThread.main(ActivityThread.java:4512) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 java.lang.reflect.Method.invoke(Method.java:511) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982) 04-21 07:00:48.690: E/AndroidRuntime(23874): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:749) 04-21 07:00:48.690: E/AndroidRuntime(23874): at dalvik.system.NativeStart.main(Native Method) 04-21 07:00:48.690: E/AndroidRuntime(23874): Caused作者:java.lang。NullPointerException 04-21 07:00:48.690: E/AndroidRuntime(23874): at com.arabicalphabets.reham.Wordgame2.onCreate(Wordgame2.java:36) 04-21 07:00:48.690: E/AndroidRuntime(23874):在 android.app.Activity.performCreate(Activity.java:4465) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 04-21 07 :00:48.690: E/AndroidRuntime(23874): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 04-21 07:00:48.690: E/AndroidRuntime(23874): ... 还有 11 个在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 04-21 07 :00:48.690: E/AndroidRuntime(23874): ... 11 更多在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 04-21 07:00:48.690: E/AndroidRuntime(23874): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 04-21 07 :00:48.690: E/AndroidRuntime(23874): ... 11 更多

4

2 回答 2

0

首先不是单独声明每个按钮,而是使用循环来声明它们......

Button[] buttons; 
for(int i=0; i<5; i++) {  //for your 5 buttons
{
 String buttonID = "bu" + (i+1);
 int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
 buttons[i] = ((Button) findViewById(resID));
}

- 使用 switch() 找到点击的按钮...

- 并作为案例实施onclick()如下

public void onClick(View v) {
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString()); 
}

希望它有效

于 2013-04-20T06:43:22.133 回答
0

我看不出它有什么问题,为什么它会崩溃,为什么该if else语句不起作用......??

public class Wordgame2 extends Activity implements OnClickListener {

Button[] buttons; 
TextView t1;
ImageView i1, i2;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    // Full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.wordgame2);

    for(int i=0; i<5; i++) {  
        {
         String buttonID = "bu" + (i+1);
         int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
         buttons[i] = ((Button) findViewById(resID));
         buttons[i].setOnClickListener(this);
        }
        }
    t1 = (TextView) findViewById(R.id.t1);

    i1 = (ImageView) findViewById(R.id.i1);
    i2 = (ImageView) findViewById(R.id.i2);







}
public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()){

    case R.id.bu1:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu2:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu3:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

    case R.id.bu4:
        t1.setText(t1.getText().toString()+" "+buttons[i].getText().toString());
        break;

//当他们点击bu5时,如果t1正确为“abc d”,它将显示图片

    case R.id.bu5:


        if(t1.getText().toString().equals("a b c d")) //this is not working
        {

            i1.setVisibility(View.VISIBLE);
            i2.setVisibility(View.INVISIBLE);
        }       

        else {
            i2.setVisibility(View.VISIBLE);
            i1.setVisibility(View.INVISIBLE);
        }

        break;

    }
}

}

于 2013-04-20T16:37:30.443 回答