Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';按下按钮时,不要更改文本大小_Android_Xml_Button_Layout_Text Size - Fatal编程技术网

Android Can';按下按钮时,不要更改文本大小

Android Can';按下按钮时,不要更改文本大小,android,xml,button,layout,text-size,Android,Xml,Button,Layout,Text Size,以下是我在main_activity.xml中的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" andro

以下是我在main_activity.xml中的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"
    android:weightSum="4" >
    <EditText
        android:id="@+id/result_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:orientation="vertical"
        android:weightSum="4" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4"
            android:layout_weight="1"
            android:orientation="horizontal" >
           <Button
                android:id="@+id/Btn7_id"
                style="@style/ButtonTextWhite"
                android:textColor="@drawable/buttontextcolorwhite"
                android:background="@drawable/custombutton"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="7"
                android:layout_weight="1"
                android:onClick="btn7Clicked" />
            <Button
                android:id="@+id/Btn8_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="8"
                android:layout_weight="1"
                android:onClick="btn8Clicked" />
            <Button
                android:id="@+id/Btn9_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="9"
                android:layout_weight="1"
                android:onClick="btn9Clicked" />
            <Button
                android:id="@+id/Btnclear_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="C"
                android:layout_weight="1"
                android:onClick="btnclearClicked" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4"
            android:layout_weight="1"
            android:orientation="horizontal" >
            <Button
                android:id="@+id/Btn4_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="4"
                android:layout_weight="1"
                android:onClick="btn4Clicked" />
            <Button
                android:id="@+id/Btn5_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="5"
                android:layout_weight="1"
                android:onClick="btn5Clicked" />
            <Button
                android:id="@+id/Btn6_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="6"
                android:layout_weight="1"
                android:onClick="btn6Clicked" />
            <Button
                android:id="@+id/Btnplus_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="+"
                android:layout_weight="1"
                android:onClick="btnplusClicked" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4"
            android:layout_weight="1"
            android:orientation="horizontal" >
             <Button
                android:id="@+id/Btn1_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="1"
                android:layout_weight="1"
                android:onClick="btn1Clicked" />
            <Button
                android:id="@+id/Btn2_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="2"
                android:layout_weight="1"
                android:onClick="btn2Clicked" />
            <Button
                android:id="@+id/Btn3_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="3"
                android:layout_weight="1"
                android:onClick="btn3Clicked" />

            <Button
                android:id="@+id/Btnminus_id"
                style="@style/ButtonTextWhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:onClick="btnminusClicked"
                android:text="-" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:layout_weight="1"
            android:orientation="horizontal" >
            <Button
                android:id="@+id/Btnequal_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:text="="
                android:layout_weight="1"
                android:onClick="btnequalClicked" />

            <Button
                android:id="@+id/Btnmulti_id"
                style="@style/ButtonTextWhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:onClick="btnmultiClicked"
                android:text="*" />

            <Button
                android:id="@+id/Btndivide_id"
                style="@style/ButtonTextWhite"
                android:background="@drawable/custombutton"
                android:textColor="@drawable/buttontextcolorwhite"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:onClick="btndivideClicked"
                android:text="/"
                android:layout_weight="1" /> 
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@drawable/unpressedbutton">

    </item>
    <item android:state_pressed="true" android:drawable="@drawable/pressedbutton">

    </item>
</selector>
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#aaaaaa"/> <!-- pressed -->

    <item android:state_pressed="false" android:color="#ffffff"/> <!-- default -->
</selector>

但它不起作用。。。这个按钮真的不起作用了


我建议通过编程而不是在xml文件中进行尝试。在activity类中,初始化要更改文本大小的EditText,并添加如下所示的onlick方法

EditText text = new EditText(this);

    btnChangeTextSize = (Button) findViewById(R.id.Btn1_id);
    btnChangeTextSize.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        text.setTextSize(10);   
        }
    });

实现此代码时,请确保按ctrl+shift+o键导入所需的类。

请参阅此链接以获取答案。您是说关于单击事件的第二个答案吗?我已尝试添加单击事件,但没有任何结果。。。无论如何,谢谢你。我想我是绿色的……非常感谢你的回答,这听起来不错,但我无法实现。我已将代码添加到我的问题中。
package com.example.simplecalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

    public String str ="";
    Character op = 'q';
    int i,num,numtemp;
    EditText showResult;
    Button btnChangeTextSize;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.showResult = (EditText)findViewById(R.id.result_id);

        final EditText text = new EditText(this);

        this.btnChangeTextSize = (Button) findViewById(R.id.Btn1_id);
        this.btnChangeTextSize.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                text.setTextSize(6);   
            }
        });

    }
    public void btn1Clicked(View v){
        insert(1);
    }
EditText text = new EditText(this);

    btnChangeTextSize = (Button) findViewById(R.id.Btn1_id);
    btnChangeTextSize.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        text.setTextSize(10);   
        }
    });