【问题标题】:Android Calculator App - Some QuestionsAndroid 计算器应用程序 - 一些问题
【发布时间】:2013-01-05 16:50:26
【问题描述】:

我是 android 应用程序开发的新手。我正在尝试创建一个计算器应用程序,我有 2 个问题要问。

1. 如何在 LinearLayout 中创建新行? 它目前看起来像这样:

[文本视图][文本视图][按钮]

我希望它看起来像这样:

[文本视图][文本视图]

[按钮]

这是我的 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/number1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/number1" />

<TextView
    android:id="@+id/number2"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/number2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_calculate" />

2.如何处理“number1”和“number2”Textview 的总和并将其显示在 Main Activity(TextView 所在的位置)上?

【问题讨论】:

    标签: android android-linearlayout calculator


    【解决方案1】:

    线性布局可以是水平或垂直的,如果您需要行和列,则需要使用相对布局,或者只添加一个以上的线性布局,例如......

    TextView txtvar = (TextView) findViewById(R.id.textViewIdFromXml);
    txtvar.setText("blah");
    

    这是您连接到带有 id textViewIdFromXml 的 textview 元素并为文本设置 blah 的方式

    【讨论】:

    • 如何将我的代码更改为 RelativeLayout 以便我可以处理多行?我试过没有成功。
    • 在 xml -root 布局中
    • 你可以随意组合它
    • 好的,谢谢,您对第二个问题有任何想法吗? :)
    • 谢谢。是否只有文本输出到屏幕而不是输入框(TextView)?
    【解决方案2】:
    1. 嵌套线性布局。

    例如(伪xml):

    <Linear Layout : Vertical>
       <Linear Layout : Horizontal>
           <TextView />
           <TextView />
       </Linear Layout>
       <Button />
    </Linear Layout>
    

    编辑:我在 Eclipse 中尝试了以下操作,对我来说,它可以满足您的要求(尽管它看起来并不花哨):

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />
    
        </LinearLayout>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    </LinearLayout>
    

    【讨论】:

    • 我照你说的做了:[VERTICAL] [HORIZONTAL] [textview] [textview] [/HORIZONTAL] [button] [/VERTICAL] 但是按钮没有出现,只有 TextViews 出现.
    • 我很好奇并在 Eclipse 中尝试了它。我用一些真正的 xml 进行了编辑。
    • 是的,我只是第一个 LinearLayout 出现问题,它覆盖了按钮,现在它可以正常工作了,谢谢。
    【解决方案3】:

    第一个答案: 创建一个垂直线性布局并使用嵌套在垂直布局中的水平布局。但我更喜欢相对布局。

    2ns Ans: 使用onClickListener() 作为按钮。 & 在每个onClickListener() 下使用setText() 更改数字显示文本框的文本。但是在 = 按钮的 onClickListener() 上,使用 getText() 的数字显示文本框的值并将其用于 + - 或 * /

    【讨论】:

    • 谢谢,我使用了“android:onClick” 我终于可以正常工作了谢谢大家:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多