草庐IT

android - 按下按钮时无法更改文字大小

coder 2024-07-02 原文

这是我在 ma​​in_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>

http://postimg.org/image/72zhaeai3/28339a2a/

我使用层列表创建了自定义按钮,在不同状态下具有不同的外观(res/drawable/unpressedbutton.xml 和 res/drawable/pressedbutton.xml)。这是 res/drawable/custombutton.xml:

<?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>

然后我以几乎相同的方式处理默认状态和按下状态之间的文本颜色变化。这是 res/drawable/buttontextcolorwhite.xml:

<?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 version="1.0" encoding="utf-8"?>

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

    <item android:state_pressed="false" android:textSize="12"/> <!-- default -->
</selector>

我怎样才能做到这一点?


我已经根据 Drakora 的回答更改了我的代码:

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);
    }

...

但是它不起作用...而且按钮实际上不再起作用了吗?

最佳答案

我建议以编程方式而不是在 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 导入您需要的必要类。

关于android - 按下按钮时无法更改文字大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22659672/

有关android - 按下按钮时无法更改文字大小的更多相关文章

  1. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  2. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  10. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

随机推荐