【问题标题】:Android App - Radio Buttons, TextView, and Layout OptionsAndroid 应用 - 单选按钮、TextView 和布局选项
【发布时间】:2012-09-29 08:50:06
【问题描述】:

我正在创建我的第一个 Android 应用程序。这似乎相当简单。但我完全是 Java 和 Android 的菜鸟(我更熟悉 C、C++ 等)。如果这是有史以来最愚蠢的问题,我很抱歉。无论如何,我按照android dev网站上的步骤进行操作。

该应用程序应该让用户输入他们的姓名并点击第 1、第 2 或第 3 班单选按钮,当他们点击停机按钮时,他们将被带到另一个页面(活动),显示他们的姓名和他们选择的班次,然后显示另一个文本框和时间输入。

到目前为止,我的 MainActivity.java 是这样完成的:

package com.cyapps.downtime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.cyapps.downtime.MESSAGE";

    public void clickedButton1(View view) {
        Intent intent = new Intent(this, WinderDTActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    public void clickedButton2(View view) {
        Intent intent = new Intent(this, ClamperDTActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

    public void clickedButton3(View view) {
        Intent intent = new Intent(this, OtherDTActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

和这样的activity_main.xml:

<RelativeLayout 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="horizontal" >

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:ems="10"
        android:hint="@string/edit_message" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_message"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="@string/radio_button1" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioButton1"
        android:layout_centerHorizontal="true"
        android:text="@string/radio_button2" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioButton2"
        android:layout_centerHorizontal="true"
        android:text="@string/radio_button3" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_centerHorizontal="true"
        android:text="@string/button_send1"
        android:onClick="clickedButton1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_centerHorizontal="true"
        android:text="@string/button_send2"
        android:onClick="clickedButton2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:text="@string/button_send3"
        android:onClick="clickedButton3" />

</RelativeLayout>

当您完成输入您的姓名并单击班次时,我现在会显示另一个活动。该页面应该显示您的姓名和班次号码,并有一个文本框可以在其中写一些其他内容,还有一个时间输入和一个提交按钮。我知道如何做按钮,我在 Eclipse 的界面上看到了时间输入。但我不明白如何使单选按钮能够“提交”并显示在页面上,以及如何编辑活动以显示某些内容。我很困惑。

这就是 WinderDTActivity.java 的样子:

package com.cyapps.downtime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class WinderDTActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(20);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }
}

这就是 activity_winder_dt.xml 的样子:

<RelativeLayout 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" >    
</RelativeLayout>

如果您能帮助我,请提前非常感谢您。你不知道我会多么感激。我一直在努力理解这一点,但我对如何让 xml 和 java 一起工作感到非常困惑。请帮忙!

【问题讨论】:

    标签: java android xml radio


    【解决方案1】:

    您已经将文本框的内容发送到第二个活动,对吗?然后你只需要再做两件事来发送单选按钮状态:

    1. 为单选按钮实现 onClick 处理程序。您已经为提交按钮完成了该操作,所以我假设您已经了解了 onClick 的内容。在单选按钮 onClick 中,在 MainActivity 中设置一个变量。

    2. 将无线电标志设置的变量打包到意图的额外数据中,如下所示:

      intent.putExtra("radioButtonState", radioButtonState);
      

      您可以在第二个活动中使用适当的intent.getxxxxExtra() 函数(例如,如果您保存了一个 int,则为getIntExtra)读回结果。

    【讨论】:

    • 哦,好吧。我已经实现了 onClick 处理程序。在我的第二个活动中,我将 `intent.getStringExtra(MainActivity.radioButtonState);` 放在我的另一个意图下,该意图获得了操作员的名字。但是现在当我尝试运行它时,它甚至没有像以前那样显示操作员的名字。 :( 如果这很烦人和荒谬,我很抱歉。
    • 看看documentation for Intent。请注意,尽管有许多预定义的额外字符串,例如EXTRA_TITLE,但Intent.putExtra(String, String) 的接口将采用任何字符串。它前面应该有包名,但是你定义的任何字符串都可以。
    • 更具体地说,在任何地方添加几个字符串常量:public static final String EXTRA_NAME = "com.example.myApplication.name";public static final String EXTRA_RADIO_STATE = "com.example.myApplication.radioState";。稍后,在创建 Intent 时:intent.putExtra(EXTRA_NAME, name); intent.putExtra(EXTRA_RADIO_STATE, radioButtonState);。在活动 2 的 onCreate 中:String name = intent.getStringExtra(EXTRA_NAME); int radioButtonState = intent.getIntExtra(EXTRA_RADIO_STATE);
    • 你好,我对这段代码还有点不理解。你介意再回答我几个问题吗?如果没有,我可以提出一个新问题。但看起来你真的很清楚自己在做什么!我真的很感谢你的帮助。但我不确定这件事是否有点拥挤。我可以给你发电子邮件吗?
    • 它越来越拥挤,但 stackoverflow 似乎没有私人消息(违背了网站的理念),而且我不喜欢公开发布我的电子邮件地址。太挤了!说实话,我真的不知道自己在做什么——我大约一个月前才开始 Android 开发,我在自己的代码中犯了错误,我只是因为你的问题才发现查看 API 文档!所以我现在也需要修复我的代码。
    【解决方案2】:

    你不能在onCreate方法中写Button b1=(Button)findViewById(R.id.Button1)

    这就是你没有得到按钮点击事件的原因

    类似地为button2和button3编写代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-23
      • 2012-04-21
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      相关资源
      最近更新 更多