【问题标题】:Beginner Android Strings textView: How to store, display and toggle -> Flashcard app初学者 Android Strings textView:如何存储、显示和切换 -> 抽认卡应用
【发布时间】:2014-11-12 20:29:43
【问题描述】:

我需要一些帮助来在我的抽认卡应用中存储和显示文本字符串。计划是当用户点击 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) {
    }



}

【问题讨论】:

    标签: java android xml android-activity


    【解决方案1】:

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

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

    【讨论】:

    • 感谢 HMiller,我正在研究 SQLite,但首先我试图在带有预存储字符串的卡片之间导航。在更基础的第 1 层:如何在 2 个静态字符串之间进行 onClick textView 切换,然后如何使用 SQLite 操作该 onClick 函数
    • 如果你让我看看你的代码,我可能会给你一些建议
    • 这会很有帮助,我会在 5 小时后下班,然后将所有内容上传到 GitHub,这样可以吗?
    • 我已经发布了一些应该引导您朝着正确方向前进的内容,但很遗憾,我将在大约 12 小时后无法回复,所以希望这足以让您继续前进。从那时起,您只需要交换字符串等
    【解决方案2】:

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

    假设您在布局 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
    }
    

    【讨论】:

    • 您好,抱歉耽搁了。我在实现这一点时遇到了一些问题,因为使用 View.Id 的开关盒只有在我有多个 textView(多个 ID)时才有效,就我而言,我有1 个 textView (到目前为止),我无法比较 View 的字符串。我进行了更改并取得了一些进展,如果您能看一下,我将不胜感激:stackoverflow.com/questions/27038321/…
    猜你喜欢
    • 1970-01-01
    • 2013-12-31
    • 2021-07-05
    • 2015-02-18
    • 2021-09-25
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多