繁体   English   中英

将内容视图从垂直更改为水平后,Android Button事件不起作用

[英]Android Button Event not Working after changing content view vertical to horizontal

我是Android新手。 我正在使用基本的计算器。该计算器具有两种视图模式。 一种是水平的,另一种是垂直的。 当我启动应用程序时,按钮事件运行正常。 但是,当我更改内容视图(另一个xml视图)时,事件不起作用。 我该如何解决? 请帮我。 Java代码:

    package com.habibur.app;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Display;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "MyTag";

    private EditText resultTextField;
    private int screenHeight;
    private int screenWidth;
    private Button oneButton;
    private Button twoButton;
    private Button threeButton;
    private Button fourButton;
    private Button fiveButton;
    private Button sixButton;
    private Button sevenButton;
    private Button eightButton;
    private Button nineButton;
    private Button zeroButton;
    private Button pointButton;
    private Button plusButton;
    private Button minusButton;
    private Button mulButton;
    private Button divButton;
    private Button equalButton;
    private String reqularExpression = "";
    private double num1;
    private double num2;
    private double result;
    private Button plusMinusButton;
    private Button scientificButtton;
    private Button backButton;
    private Button cancelButton;
    private boolean checkButtonColor;

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

        Display display = getWindowManager().getDefaultDisplay(); 
        screenHeight = display.getHeight();
        screenWidth = display.getWidth();
        resultTextField = (EditText)findViewById(R.id.resultTextField);
        resultTextField.setHeight((screenHeight/4));
        resultTextField.setInputType(0);

        oneButton = (Button)findViewById(R.id.button_1);
        twoButton = (Button)findViewById(R.id.button_2);
        threeButton = (Button)findViewById(R.id.button_3);
        fourButton = (Button)findViewById(R.id.button_4);
        fiveButton = (Button)findViewById(R.id.button_5);
        sixButton = (Button)findViewById(R.id.button_6);
        sevenButton = (Button)findViewById(R.id.button_7);
        eightButton = (Button)findViewById(R.id.button_8);
        nineButton = (Button)findViewById(R.id.button_9);
        zeroButton = (Button)findViewById(R.id.button_0);
        pointButton = (Button)findViewById(R.id.button_point);
        plusButton = (Button)findViewById(R.id.button_plus);
        minusButton = (Button)findViewById(R.id.button_minus);
        mulButton = (Button)findViewById(R.id.button_mul);
        divButton = (Button)findViewById(R.id.button_div);
        equalButton = (Button)findViewById(R.id.button_equal);
        plusMinusButton = (Button)findViewById(R.id.button_plus_minus);
        scientificButtton = (Button)findViewById(R.id.button_scintifiq);
        backButton = (Button)findViewById(R.id.button_back);
        cancelButton = (Button)findViewById(R.id.button_cancel);

        oneButton.setOnClickListener(this);
        twoButton.setOnClickListener(this);
        threeButton.setOnClickListener(this);
        fourButton.setOnClickListener(this);
        fiveButton.setOnClickListener(this);
        sixButton.setOnClickListener(this);
        sevenButton.setOnClickListener(this);
        eightButton.setOnClickListener(this);
        nineButton.setOnClickListener(this);
        zeroButton.setOnClickListener(this);
        pointButton.setOnClickListener(this);
        plusButton.setOnClickListener(this);
        minusButton.setOnClickListener(this);
        mulButton.setOnClickListener(this);
        divButton.setOnClickListener(this);
        equalButton.setOnClickListener(this);
        plusMinusButton.setOnClickListener(this);
        scientificButtton.setOnClickListener(this);
        backButton.setOnClickListener(this);
        scientificButtton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);

        Log.d(TAG, "Habibur Rahman");

    }

    protected void onStart(){
        super.onStart();

        this.setButtonAnimation();

    }

    protected void onResume(){
        super.onResume();

    } // end method

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "AIUB gerat");
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_main_horiozontal);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
             setContentView(R.layout.activity_main);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();
        if(id == oneButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=1;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == twoButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=2;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == threeButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=3;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == fourButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=4;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == fiveButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=5;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == sixButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=6;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == sevenButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=7;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == eightButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=8;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == nineButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=9;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == zeroButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+=0;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == pointButton.getId()){
            this.setButtonColorChanges();
            reqularExpression +=".";
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == plusButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+="+";
            resultTextField.setText(this.reqularExpression);
            checkButtonColor = true;
            plusButton.setBackgroundColor(getResources().getColor(R.color.button_forground_color));
        }
        else if(id == minusButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+="-";
            resultTextField.setText(this.reqularExpression);
            checkButtonColor = true;
            minusButton.setBackgroundColor(getResources().getColor(R.color.button_forground_color));
        }
        else if(id == mulButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+="*";
            Log.d(TAG, "CLickeddssdfsdfsdfd");
            resultTextField.setText(this.reqularExpression);
            checkButtonColor = true;
            mulButton.setBackgroundColor(getResources().getColor(R.color.button_forground_color));
        }
        else if(id == divButton.getId()){
            this.setButtonColorChanges();
            reqularExpression+="/";
            resultTextField.setText(this.reqularExpression);
            checkButtonColor = true;
            divButton.setBackgroundColor(getResources().getColor(R.color.button_forground_color));
        }
        else if(id == scientificButtton.getId()){
            this.setButtonColorChanges();
            reqularExpression+="%";
            resultTextField.setText(this.reqularExpression);
            checkButtonColor = true;
            scientificButtton.setBackgroundColor(getResources().getColor(R.color.button_forground_color));
        }
        else if(id == equalButton.getId()){
            this.setequalButtonAnimation();
            int plus = reqularExpression.indexOf('+');

        }
        else if(id == cancelButton.getId()){
            this.setButtonColorChanges();
            this.reqularExpression = "";
            resultTextField.setText(this.reqularExpression);
            this.addCancelButtonAnimation();
        }
        else if(id == backButton.getId()){
            this.setBackButtonAnimation();
            this.setButtonColorChanges();
            String temp = reqularExpression.substring(0, (this.reqularExpression.length()>0 ? (reqularExpression.length()-1) : 0));
            this.reqularExpression = temp;
            resultTextField.setText(this.reqularExpression);
        }
        else if(id == plusMinusButton.getId()){
            this.setButtonColorChanges();
            Toast.makeText(getApplicationContext(), "This feature wil come in final version", Toast.LENGTH_SHORT).show();
        }
        else{

        }

        Log.d(TAG, "CLicked");
    } // end method

    private void setButtonAnimation(){
        Animation buttonAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.button_animation);


        sevenButton.startAnimation(buttonAnimation);
        eightButton.startAnimation(buttonAnimation);
        nineButton.startAnimation(buttonAnimation);

        fourButton.startAnimation(buttonAnimation);
        fiveButton.startAnimation(buttonAnimation);
        sixButton.startAnimation(buttonAnimation);

        oneButton.startAnimation(buttonAnimation);
        twoButton.startAnimation(buttonAnimation);
        threeButton.startAnimation(buttonAnimation);

        zeroButton.startAnimation(buttonAnimation);
        pointButton.startAnimation(buttonAnimation);
        plusMinusButton.startAnimation(buttonAnimation);




    } // end method

    private void setButtonColorChanges(){
        if(checkButtonColor){
            plusButton.setBackgroundColor(getResources().getColor(R.color.button_color));
            minusButton.setBackgroundColor(getResources().getColor(R.color.button_color));
            mulButton.setBackgroundColor(getResources().getColor(R.color.button_color));
            divButton.setBackgroundColor(getResources().getColor(R.color.button_color));
            scientificButtton.setBackgroundColor(getResources().getColor(R.color.button_color));
        }
    } // end method

    private void addCancelButtonAnimation(){
        Animation buttonAnimation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.button_animation_2);
        cancelButton.startAnimation(buttonAnimation2);
    } // end method

    private void setBackButtonAnimation(){
        Animation buttonAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.back_button_anim);
        backButton.startAnimation(buttonAnimation);
    }

    private void setequalButtonAnimation(){
        Animation buttonAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.equal_button_anim);
        equalButton.startAnimation(buttonAnimation);
    }



}

XML view1(垂直):

  <LinearLayout
    android:orientation="vertical" >

    <EditText
        android:id="@+id/resultTextField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/result_text_size">

    </EditText>

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   >


    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/cancel"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_div"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/div"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_mul"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/mul"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />
        <Button
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/back_arrow"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />  
    </TableRow>
    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_7"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/seven"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_8"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/eight"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_9"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/nine"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_minus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/minus"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>

    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/four"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/five"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_6"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/six"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_plus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/plus"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>

    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/one"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/two"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/three"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_scintifiq"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/scintific_mode"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>

    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_0"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/zero"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_point"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/point"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_plus_minus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/plus_minus"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_equal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/equal"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>
</TableLayout>

</LinearLayout>

View2:水平:

  <LinearLayout

    android:orientation="vertical" >

   <EditText
        android:id="@+id/resultTextField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/result_text_size" >

    </EditText>

   <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_7"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/seven"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_8"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/eight"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_9"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/nine"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_div"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/div"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

         <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="@integer/button_weight"
            android:background="@color/button_color"
            android:text="@string/cancel"
            android:textSize="@dimen/result_text_size" />

    </TableRow>

    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/four"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/five"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_6"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/six"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_mul"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/mul"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_back"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/back_arrow"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>
    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/one"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/two"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/three"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

         <Button
            android:id="@+id/button_minus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/minus"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

       <Button
            android:id="@+id/button_scintifiq"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/scintific_mode"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>
    <TableRow android:layout_weight="@integer/button_weight">

        <Button
            android:id="@+id/button_0"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/zero"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_point"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/point"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_plus_minus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/plus_minus"
            android:textSize="@dimen/result_text_size"
            android:layout_weight="@integer/button_weight" />

        <Button
            android:id="@+id/button_plus"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/plus"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />
        <Button
            android:id="@+id/button_equal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/equal"
            android:textSize="@dimen/result_text_size"
            android:background="@color/button_color"
            android:layout_weight="@integer/button_weight" />

    </TableRow>
</TableLayout> 
</LinearLayout>

onConfigurationChanged()设置内容视图不是正确的处理方式。 如果您将两个具有相同名称的布局放在相应的res文件夹中,Android会为您完成。

将垂直布局保留在res/layout文件夹中,并将水平res/layout (与垂直名称相同)放在res/layout-land文件夹中。

然后删除整个方法onConfigurationChanged() 它应该开始工作,无需进行任何其他更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM