简体   繁体   English

如何在Java类中获取TextView值作为字符串并进行设置?

[英]How do I get the TextView value as string in java class and set it?

I don't think xml code is necessary, but here is the TextView : 我认为xml代码不是必需的,但这是TextView

<TextView
    android:id="@+id/option4"
    android:layout_width="175dp"
    android:layout_height="120dp"
    android:background = "@drawable/rounded_corner"
    android:textAppearance="?android:textAppearanceLarge"
    android:gravity="center"
    android:text="option 4"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/option3"
    android:layout_toEndOf="@+id/option3"
    android:screenOrientation="portrait"
    android:onClick="onClick4"
    android:clickable="true"/>

The last 2 lines are what detects the click. 最后两行是检测点击的内容。

My java code for that is: 我的Java代码是:

public void onClick4(View v) {
    if (option4Text.equals(Integer.toString(answer))) {
        TextView questionText = (TextView) findViewById(R.id.question);
        correctAnswer();
        questionText.setText(questionTxt + "");
    }
}

My code for option4Text in java class: 我在java类中的option4Text代码:

option4Text = (TextView)findViewById(R.id.option4);
    option4Text.setText(wrongAnswer() + "");

Where wrongAnswer() just returns a random number. 在哪里rongAnswer()仅返回一个随机数。 I think the problem is that option4Text itself isn't a string, so it doesn't have a value, after debugging I found that after clicking it is not going to inside of if statement. 我认为问题在于option4Text本身不是字符串,因此它没有值,在调试后,我发现单击后不会进入if语句内部。 So my question is that, how do I get the text that I set for option4Text and put it into a String? 所以我的问题是,如何获取为option4Text设置的文本并将其放入字符串中? what the way to use that string if if statement. if语句使用该字符串的方式是什么。

I couldn't Understand your problem clearly. 我不清楚你的问题。 But as I understand, You just want to fetch the string value of TextView from the xml file. 但是据我了解,您只想从xml文件中获取TextView的字符串值。

As I can see, you are doing some mistake. 如我所见,您正在犯一些错误。 option4Text is an object of TextView. option4Text是TextView的对象。 Therefore to fetch the String Value of it use the following code. 因此,要获取其字符串值,请使用以下代码。

public void onClick4(View v) 
{
    if (option4Text.getText().toString().equals(toString(answer))) {
        TextView questionText = (TextView) findViewById(R.id.question);
        correctAnswer();
        questionText.setText(questionTxt + "");
    }
}

option4Text.getText().toString() will give the value of TextView which is set via Xml or by java code. option4Text.getText()。toString()将给出通过Xml或Java代码设置的TextView的值。

Here problem is that option4Text is the object through which you are setting the string into your TextView instead of getting, to getting the TextView Value take anotehr String variable and get the value like: 这里的问题是option4Text是一个对象,通过该对象将字符串设置到TextView中而不是获取,要获取TextView值,请使用anotehr String变量并获取如下值:

String option4str;// take as global variable
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
option4str = option4Text.getText().toString();

Then match like: 然后匹配:

public void onClick4(View v) {
    if (option4str .equals(Integer.toString(answer))) {
        TextView questionText = (TextView) findViewById(R.id.question);
        correctAnswer();
        questionText.setText(questionTxt + "");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM