开发者

Need help with a simple concept in Android

I am a complete newb and know nothing. I'm probably very inefficient at what I have already, but that's the learning curve. I'm making an Android app, using Eclipse to do it.

The app will have 3 screens. On the first two screen is a list of buttons. The user taps on the buttons that apply to the situation and then that button disappears from that screen and appears on screen 3. Screen 2 is the same as screen 1 but with a separate list (selection still mixes with screen 3).

I am setting up 开发者_如何学JAVAscreen one, am typing out many, many strings (it's a long list) in my strings.xml. Let's assume it's a shopping list program. The strings are set up like:

<string name="vc002">Bread</string>
<string name="vc003">Butter</string>

And so on. Layout is like this:

    <?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:text="@string/v001"
    android:layout_gravity="center"
    />
<Button
 android:id="@+id/vb01"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:layout_gravity="center"
 android:text="@string/vc001"
 />
<Button
 android:id="@+id/vb02"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 android:layout_gravity="center"
 android:text="@string/vc002"
 />

And this is the following Activity, screen 1:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;



public class VenialListActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button vb01 = (Button)findViewById(R.id.vb01);
        vb01.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v){
         vb01.setVisibility(View.INVISIBLE);
         };
         ;  
         }
    );
    }       
}

And now I'm stuck. The button disappears as expected but I don't know how to make it appear on screen 3. Similarly, I'd like to be able to tap a button on screen 3 (in case of a mistap) and send it back home and make it visible again.

Anyone able to help me out? I apologise if I have broken any site rules or if I give someone a headache with my newbness. I don't exactly know what the above is actually doing, I just know that it works so I shouldn't break it. It's like learning Latin or Greek or Dvorak typing. It's bound to be slow going and I'm going to make more mistakes than not. Regardless, I do thank you for your time and patience if you've made it this far. =)


I definitely would recommend creating separate Activities for each screen. While there are exceptions, that’s just how Android apps were designed to work.

In your Button’s onClickListener(), instead of removing it (Setting it’s visibility to View.INVISIBLE will not persist across activities, so unless you explicitly handle that in your app it using onSaveInstantState() and onRestoreInstanceState() it will be visible when the user comes back to this Activity) you’re going to want to save the word in the application’s preferences and start the next Activity, be it the 2nd or 3rd.

In those Activities you should read what words the user selected via the preferences and take it from there.

There are multiple ways of doing this, but this method should work fine for you from what I can gather.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜