2014-12-05 90 views
-1

我正在研究一个应用程序,当您按下按钮时会生成随机引号(来自开关)。然后进入下一个屏幕,然后将其显示为textView。该部分起作用。我想要一个按钮,这会将用户带回主屏幕(我还打算另外有两个按钮,但这些按钮稍后会出现)。但是,该应用只显示文本或按钮,而不是两者。如何显示文本和按钮? (android)

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_comp_out); 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

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

    Button button = new Button(this); 
    button.setText("Main Menu"); 
    button.setTextColor(Color.parseColor("#ff0000")); 
    button.setHeight(75); 


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

这是我用来创建文本输出和按钮的代码。字符串消息是引用。此代码正在制作一个填充整个屏幕的按钮。

<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:background="@color/orange"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_marginEnd="5dp" 
    android:layout_marginStart="5dp" 
    android:layout_marginTop="5dp" 
    android:textColor="@color/blue" 
    android:text="@string/button_menu" /> 

</LinearLayout> 

没有java代码使得一个按钮,我用这一点,这是不使按钮任一。 (也没有将背景设置为橙色,但那不是什么大问题)关于如何将按钮放在文本下的任何想法?

回答

0

你有 setContentView()指令(第二和第三覆盖的第一个,这是为什么你看到的只是一个按钮 - 你在代码中定义了一个 - 和没有得到橙色背景 ,再)。
必须只有。

该指令用于设置应包含组件的Activity(或Fragment)布局。

尝试这样:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_comp_out); 

    TextView textview = (TextView) findViewById(R.id.text1); 
    Button button = (Button) findViewById(R.id.button1); 

    // Add a click handler for your button - choose one of the possible methods (see the link at the end of this answer). 

    // The following instructions should go inside the click handler. 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
} 

activity_comp_out.xml

<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:background="@color/orange" 
    > 
    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginEnd="5dp" 
     android:layout_marginStart="5dp" 
     android:layout_marginTop="5dp" 
     android:textColor="@color/blue" 
     android:text="@string/text_menu" 
    /> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginEnd="5dp" 
     android:layout_marginStart="5dp" 
     android:layout_marginTop="5dp" 
     android:textColor="@color/blue" 
     android:text="@string/button_menu" 
    /> 
</LinearLayout> 

几种可能的方法来处理视图(不只是按钮)点击这里说明:http://www.tutorialspoint.com/android/android_event_handling.htm

0

setContentView()是用来膨胀你的布局,你需要从你的inflat中抓取按钮对象ed布局:

Button button = (Button) findViewById(R.id.button1); 

一旦将其添加到布局中,您可以对TextView执行相同的操作。

相关问题