【发布时间】:2022-01-22 22:31:36
【问题描述】:
当我点击按钮时,会发生这种情况:
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" />
【问题讨论】:
标签: java android-studio button searchview