【问题标题】:ANDROID: Swapping strings on a single clickable TextViewANDROID:在单个可点击的 TextView 上交换字符串
【发布时间】:2014-11-20 11:21:17
【问题描述】:

闪存卡App

您好,我正在制作一个抽认卡应用程序,我想在用户点击主 fcfront_textView 时交换字符串。主要问题在于 switch 语句,因为它没有正确检测不同的情况和/或没有正确交换字符串。我尝试了我所知道的一切,下面是它从前到后交换但不会交换回来的最佳情况。

我的想法是我将预先存储问题/答案字符串,并且 onClick 应该在 Q/A 之间交换。最终我希望 nextButton/prevButton 在一对可点击的 textView 字符串 (Q/A) 之间导航,并且能够添加 QA,但我不知道如何实现。

感谢您的宝贵时间,请遵循我的代码:


.java
public class MainActivity extends splashActivity {


    Button closeButton, addButton, prevButton, nextButton;
    TextView QAText, answerText;

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

        QAText = (TextView) findViewById(R.id.fcfront_textView);
        answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm


        QAText.setOnClickListener(new View.OnClickListener()
        {
            //function to toggle between Q/A
            public void onClick(View v)
            {
                // exchange();

                //switch(Resources.getSystem().getString(R.string.fc_back))  //bad!

                 //switch(QAText.getText().toString()) //did not work: char != string
                switch(R.string.fc_front)
                {
                    case R.string.fc_front:
                        ((TextView)findViewById(R.id.fcfront_textView)).setText(R.string.fc_back);
                        Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show();
                        //QAText.setVisibility(v.GONE);
                        break;

                    case R.string.fc_back:
                        ((TextView)findViewById(R.id.fcfront_textView)).setText(R.string.fc_front);
                        Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show();
                        break;
                }
                Toast.makeText(getApplicationContext(), "OUT OF THE SWITCH", 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);
    }


    //gets click event directly from xml
    public void next_Clicked(View view)
    {
        //test:
        Toast.makeText(getApplicationContext(), "Next button works!!", Toast.LENGTH_LONG).show();
    }

    public void prev_Clicked(View view)
    {
        //test:
        Toast.makeText(getApplicationContext(), "Prev button works!!", Toast.LENGTH_LONG).show();
    }


    //supposed to swap strings (did not work)
    private void exchange()
    {
        String frontstring = getString(R.string.fc_front);
        String backstring = getString(R.string.fc_back);
        String displaystring = getString(R.string.action_settings);


        //test:
        // Toast.makeText(getApplicationContext(), "textView works!", Toast.LENGTH_LONG).show();

            if (frontstring != displaystring) {
                //displaystring =  getString(R.string.fc_back);
                ((TextView) findViewById(R.id.fcfront_textView)).setText(R.string.fc_back);

            } else {
                //displaystring = getString(R.string.fc_front);
                ((TextView) findViewById(R.id.fcfront_textView)).setText(R.string.fc_front);
            }


//switch case by id did not work:
//        switch(view.getId())
//        {
//            case R.id.fcfront_textView:
//                TextView txtView = (TextView)findViewById(R.id.fcfront_textView);
//                txtView.setVisibility(View.GONE);
//                break;
//        }
        Toast.makeText(getApplicationContext(), "end of exchange()", Toast.LENGTH_LONG).show();

    }




} //end of main


布局.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity"
    android:clickable="true"
    android:id="@+id/background"
    android:background="#ff80b5ff">

    <TextView
        android:text="@string/fc_front"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fcfront_textView"
        android:layout_centerVertical="true"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:linksClickable="true"
        android:textIsSelectable="false"
        android:singleLine="true"
        android:onClick="OnClick"

        android:clickable="true" />

    <!--<TextView-->
        <!--android:text="@string/fc_back"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_below="@+id/fcfront_textView"-->
        <!--android:layout_centerHorizontal="true"-->
        <!--android:id="@+id/fcback_textView"-->
        <!--android:textSize="30sp"-->
        <!--android:linksClickable="true"-->
        <!--android:textIsSelectable="false"-->
        <!--android:singleLine="true"-->
        <!--android:clickable="true" />-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_prev"
        android:id="@+id/prevbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="bottom|center"
        android:layout_weight="1"
        android:textSize="30sp"
        android:layout_alignParentStart="true"
        android:onClick="prev_Clicked"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_next"
        android:id="@+id/nextbutton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="bottom|center"
        android:layout_weight="1"
        android:textSize="30sp"
        android:hint="Browse +"
        android:onClick="next_Clicked"


    android:layout_alignParentEnd="true" />


<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X"
    android:id="@+id/closebutton"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:gravity="center"
    android:textSize="30sp"

    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/plusbutton"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:gravity="center"
    android:textSize="30sp"

    android:layout_alignParentStart="true" />

</RelativeLayout>


字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CSCE305HW1</string>
    <string name="fc_front">FLASHCARD: FRONT</string>
    <string name="fc_back">FLASHCARD: BACK</string>
    <string name="Q1">Question1</string>
    <string name="A1">Answer1</string>
    <string name="Q2">Question2</string>
    <string name="A2">Answer2 </string>
    <string name="action_settings">Nothing here yet</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_goodbye">goodbye</string>
    <string name="main_next">Next</string>
    <string name="main_prev">Previous</string>

</resources>

【问题讨论】:

  • 尝试使用 if-else 进行字符串比较,而不是 switch 主要不用于字符串大小写。
  • 怎么样?!到目前为止我尝试的一切都没有奏效..

标签: java android xml string textview


【解决方案1】:

尝试使用 if-else 代替 switch 进行字符串比较:

if(QAText.getText().toString().equals(getString(R.string.fc_front))){
   QAText.setText(R.string.fc_back);
   Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show();
}else{
   QAText.setText(R.string.fc_front);
   Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show();
}

【讨论】:

  • 老兄,你太棒了,我为此苦苦挣扎了好一阵子..关于如何在一对字符串之间导航的任何提示(Q/A)?
  • @b__,您能否提出新问题,提供完整信息,在一对字符串之间导航并给我链接,我会尽力解决您的问题。
  • @b__,很高兴为您提供帮助。
  • 边做边说
  • 我只能每 90 分钟发布一个问题(?!)我会在时间到期后立即发布,并将在此处使用堆栈正确链接发表评论:codeshare.io/PcoVo
【解决方案2】:

我认为您的问题在于您的 switch 声明。 switch(R.string.fc_front) 是什么意思?是这样的:

  switch (2)
  {
     case 2 : 
       ...
       break;
     case 1 :
       ...
       break;
  }

因为在R.java 文件中,每个资源都存储为一个数字。

您必须检查您的 TextView 的文本而不是 R.string.fc_front

试试这个:

 if (textView.getText().equals("string1")) {
     ...
 } else if (textView.getText().equals("string2")) {
     ...
 } ...

【讨论】:

  • 我正在尝试检查当前 textView 中的字符串以交换它。我尝试通过 id: switch(R.id.idoftextView) 做,但它没有成功,因为我试图在同一个 textView 中交换字符串(*反对不同的 textViews)。
  • 现在可以了,谢谢!我仍然需要一些关于如何在不同的字符串对之间导航的帮助..
  • 如果解决了您的问题,请接受我的回答! :)
【解决方案3】:

你应该比较字符串:

string front = getActivity().getResources().getString(R.string.fc_front);
string back = getActivity().getResources().getString(R.string.fc_back);

if (answerText.getText().toString().compareTo (front) == 0 ) {
        //do something
    } else if (answerText.getText().toString().compareTo (back) == 0 ) {
       // do something else
    }

不要使用 equals 比较字符串,而是使用 compareTo。

您应该移除开关盒并改用此 if/else。

【讨论】:

  • 这确实有效,谢谢!知道如何移动到另一对字符串吗?
  • 你真的应该使用 compareTo 而不是 equals。你想下一步该做什么?我不明白...
  • 我正在尝试使用上一个/下一个按钮在不同的字符串对之间导航:stackoverflow.com/questions/27046574/…
猜你喜欢
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-27
  • 2012-04-15
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多