當我單擊按鈕時,會發生這種情況:
https://i.stack.imgur.com/PeAAG.png
該按鈕用于在用戶填充時從 searchView 獲取值。并為打開下一個活動。點擊:
String state;
SearchView searchView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
searchView=(SearchView)findViewById(R.id.search_product);
Button get_product=findViewById(R.id.get_product);
get_product.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String product=searchView.toString();
Intent intent = new Intent(SearchActivity.this, The_End.class);
intent.putExtra("product", product);
Toast.makeText(getApplicationContext(),product,Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
這是我的帶有按鈕和 searchView 的 xml:
<SearchView
android:id="@ id/search_product"
android:layout_width="249dp"
android:layout_height="37dp"
android:background="#CCCDFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285" />
<Button
android:id="@ id/get_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.839"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/search_product"
app:layout_constraintVertical_bias="0.885" />
uj5u.com熱心網友回復:
在String product=searchView.toString();您沒有獲取 searchview 的值時,您正在獲取 searchview 的參考并將其轉換為toString()不應使用的字串。
要獲取您需要使用的 searchview 的內容,searchView.getQuery()然后將結果放入您的 intent.putExtra("product", searchView.getQuery());
uj5u.com熱心網友回復:
String product = searchView.toString();
為您使用的 SearchView 提供 viewId。而不是從您需要使用的 searchView 獲取文本
String product = searchView.getQuery().toString();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/391045.html
上一篇:你如何制作一個并排的可折疊按鈕?
