初学者Android Strings textView:如何存储,显示和切换 - > Flashcard应用程序

时间:2014-11-12 20:29:44

标签: java android xml android-activity

我需要一些帮助,在我的flashcard应用程序中存储和显示文本字符串。计划是当用户点击textView(FLASHCARD_FRONT)时,它切换到FLASHCARD_BACK,并能够在不同的抽认卡(正面+背面)之间导航:

screenshot

理想情况下,我希望将字符串保留在数组中,以便将来用户可以添加新字符串(卡片)并通过它们进行导航。

public class MainActivity extends Activity {


    Button closeButton, addButton, prevButton, nextButton;
    TextView question, answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        closeButton = (Button) findViewById(R.id.closebutton);
        addButton = (Button) findViewById(R.id.plusbutton);
        prevButton = (Button) findViewById(R.id.prevbutton);
        nextButton = (Button) findViewById(R.id.nextbutton);

        question = (TextView) findViewById(R.id.fcfront_textView);
        answer = (TextView) findViewById(R.id.fcfront_textView); //not linked to anywhere atm




        //adicionado
        nextButton.setOnClickListener(new View.OnClickListener()
        {
          @Override
          public void onClick(View view) {
              Toast.makeText(getApplicationContext(), "Hello TOAST test!", Toast.LENGTH_LONG).show();

          }
        });
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void onPlusButtonClick(View view) {
    }

    public void onCloseButtonClick(View view) {


        String close_msg = "Thanks for trying !! " ;

        Toast.makeText(this, close_msg, Toast.LENGTH_SHORT).show();
    }

    public void onNextButtonClick(View view) {
    }

    public void onPrevButtonClick(View view) {
    }



}

2 个答案:

答案 0 :(得分:0)

你可以用很多不同的方式做这样的事情,你可以使用类似HashMap的东西,它允许你存储一个带有键值的键(一个词可以是键,定义值) ),您可以创建一个SQLite数据库,以便将卡存储在设备上,并在应用程序的每次启动时轻松重新加载。

我建议查看SQLite数据库,因为它允许您添加,删除甚至更新卡。此信息也存储在设备上,因此只要应用程序存在,它就会一直存在。

答案 1 :(得分:0)

首先,我建议您将布局文件中的所有按钮操作点击定义为相同的内容,然后您可以执行此操作:

假设您在布局xml文件中按照以下步骤进行所有操作点击

android:onClick="action_Clicked"

然后您可以通过这种方式访问​​这些方法

public void action_Clicked(View view){

switch(view.getId()){

    case R.id.fcfront_textView:
        exchange()
        break;

    case R.id.button2:

        break;

    case ...and so on..:

       break;
    }
}

这样就无需以您拥有的方式创建按钮,您可以删除以下所有内容

button = (Button) findViewById(...);

然后,回到点击的动作,假设你想在点击fcfront_textView时做点什么

    public void action_Clicked(View view){

    switch(view.getId()){

        case R.id.fcfront_textView:
            exchange();
            break;
        }
    }

请注意,当单击fcfront_textView时,我们调用exchange()方法,因此我们需要创建该方法

private void exchange(){
    //TODO exchange strings
    //we can do this by checking what the currently set string is, and 
    //if it is set as string1, set it as string 2, else set as string1
}
相关问题