ANDROID:单击按钮更改TextView

时间:2011-08-09 21:42:41

标签: android button

首先,我是Android编程的新手,所以我的编码水平不是太高:p 我有一副纸牌类,其中我想将卡片的值返回到文本视图中。

cardValue = "The " + numString + " of " + suitString;
    return cardValue;

这是我的deck类中drawCard方法的结束。 在我的virtualDeck类中,我创建了这个类的实例,

final textdeck deck = new textdeck();
        String value = deck.drawCard(); 

我要做的是在virtualDeck类上创建一个按钮,一旦点击它将从textDeck类运行drawCard方法,并将结果返回到textView。每次单击按钮时,都会更改textView以打印新值。

我似乎很想知道如何做到这一点。 任何帮助深表感谢。 欢呼声。

2 个答案:

答案 0 :(得分:0)

我希望这个如何设置TextView的示例有助于:

在你的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/card"/>
</LinearLayout>

在您的来源(TextViewExample.java)

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


public class TextViewExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Do all your deck setup here
        String value = "8 of Spades";

        //New textView
        TextView cardDisplay = (TextView)findViewById(R.id.card);

        //Update Text
        cardDisplay.setText(value);

    }
}

答案 1 :(得分:0)

我不太明白你想要做什么,但这是你通过按钮改变视图的方式:

    Button nextButton =(Button)findViewById(R.id.next);
    nextButton.setOnClickListener(this);

下一步是在当前布局中创建的Button。 然后在onclick监听器中:

    Intent intent = new Intent(this,SecondIntent.class);                                
    startActivity(intent);
    finish();

和SecondIntent是您要调用的第二个类的名称。该类可以有自己的视图,因为它可以调用setContentView()。

唯一要确保打开AndroidManifest.xml并在标记中添加以下内容

<activity android:name=".SecondIntent" android:label="@string/app_name"> </activity>

将其添加到清单会让android知道其他活动存在。

现在,如果您想打开第二个视图并能够关闭第二个视图以返回第一个视图,只需将finish()添加到第二个活动而不是第一个活动。

祝你好运! - Android Noob研究员

相关问题