gpt4 book ai didi

java - 无法从编辑文本小部件 (android) 获取文本

转载 作者:行者123 更新时间:2023-11-30 10:27:46 26 4
gpt4 key购买 nike

我不知道我做错了什么,但是当我从编辑文本打印文本时(使用 getText().toString()),在 logcat 中它始终是一个空字符串。我想知道这是否与它在继续按钮的 onClick 函数中完成有关。我将整个 onCreate 函数放在一起,因为 onClick 函数中的代码似乎与无数教程中显示的内容完全匹配。

@Override
protected void onCreate(Bundle savedInstanceState)
{

Button add_test = new Button(this);
Button delete_test = new Button(this);
Button[] barray = new Button[100];
int trans_grey = Color.parseColor("#40000000");
final String[] test_types = new String[] {"Choose...","Terms", "Multiple choice", "Custom"};
final ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,test_types);

// Preliminary operations and display opening layouts
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout scroll_layout = (LinearLayout)findViewById(R.id.scroll_layout);
LayoutParams scroll_params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);


// Add the add a dynamic button
for (int index = 0; index <= 4; index++)
{
barray[index] = new Button(this);
barray[index].setBackgroundColor(trans_grey);
barray[index].setText("Array buttons");
barray[index].setTextColor(Color.parseColor("#CCffffff"));
scroll_layout.addView(barray[index], scroll_params);
}


add_test.setTextColor(Color.parseColor("#CCffffff"));
add_test.setBackgroundColor(trans_grey);
add_test.setText("Add a Test");
scroll_layout.addView(add_test, scroll_params);
add_test.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v)
{

AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);
final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null);
Spinner type_spinner = (Spinner) add_test_view.findViewById(R.id.type);
add_test_builder.setView(add_test_view);
final Button continue_button = (Button) add_test_view.findViewById(R.id.continue_button);
AlertDialog dialog = add_test_builder.create();
dialog.show();
dialog.getWindow().setLayout(950,900);

type_spinner.setAdapter(type_adapter);
continue_button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
View add_test_view = getLayoutInflater().
inflate(R.layout.add_test_menu, null);
// The view of the test attribute dialog
EditText test_title = (EditText) add_test_view.
findViewById(R.id.testTitle);
// Test title widget to hold title of test
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title);

}
});
return;
}
});

// Add the delete a test button
delete_test.setTextColor(Color.parseColor("#CCffffff"));
delete_test.setBackgroundColor(trans_grey);
delete_test.setText("Delete a Test");
scroll_layout.addView(delete_test, scroll_params);


return;
}

}

布局:

<EditText
android:id="@+id/testTitle"
android:maxLines="1"
android:lines="1"
android:inputType="textAutoCorrect"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#00000000"
android:hint="Test Title"/>

最佳答案

不要多次膨胀您分配给对话框的 View ,因为您正在创建它的多个实例,您正在写入一个 View 但试图从另一个 View 获取数据....尝试这样做......

AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);

final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null); 

final EditText test_title = (EditText) add_test_view. findViewById(R.id.testTitle);

continue_button.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title); } });

关于java - 无法从编辑文本小部件 (android) 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com