我对编程比较陌生,目前我希望有一个用户填写的“自动完成文本视图”,以影响最终的文本结果。我认为我可以做一个简单的'if'函数字符串,但这似乎不起作用,因为它显然需要一个'表达式',一个布尔值或一个字符串。说实话,我正在努力弄清楚我究竟需要写什么 - 我是否需要定义文本框功能本身等等 - 我想这就是为什么我遇到这个问题。
这是我到目前为止所做的:
(在mainactivity.java中)
package com.example.danielsaleh.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
// In order for us to have the text-view display and change according to what the person clicked in the textbox, we need an Array list, which is done below
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final_text =(TextView) findViewById(R.id.final_answer);
final_text.setEnabled(false);
changeTextOnce();
// This is the code to find the string variable
String[] Exam_Board;
// We need a text view here
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.exam);
// We need to get the string array here in order to make the auto-complete (linked to strings.xml), so we do the below code
Exam_Board = getResources().getStringArray(R.array.exam_board);
// We need some place to store the text, following the previous comment. The way to do this is by storing the 'array' which is done as below
ArrayAdapter<String> exam = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Exam_Board);
// Now we need to set the view from the string, which is from the adapter. The name entitled in this case is 'exam' so we use the 'setadapter' method and type exam
autoCompleteTextView.setAdapter(exam);
然后:(这是我的问题所在)。上面的代码是自动完成文本视图
public void changeTextOnce(){
final TextView changingText = (TextView) findViewById(R.id.changingtext);
Button changeTextButton = (Button) findViewById(R.id.butto);
changeTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if () {
}
changingText.setText("The answer is.....");
}
if()是问题所在......我似乎无法解决这个问题!
有关的实际字符串代码:
<string-array name="exam_board">
<item>CIE</item>
<item>EDEXCEL</item>
<item>OCR</item>
<item>AQA</item>
</string-array>
如果需要(虽然我认为不需要它但是我没有足够的经验告诉自己!)。 activity.main文件是:
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exam"
android:hint="Type of Exam Board"
android:layout_marginStart="55dp"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/changingtext"
android:layout_alignEnd="@+id/topic"
android:layout_alignStart="@+id/butto"
android:layout_below="@+id/butto" />
值得注意的是{错误只是因为我省略了与我的问题无关的代码部分,因为否则这篇文章会如此之大,任何人甚至回答我的问题都是绝对荒谬的!
谢谢,祝周末愉快!