在以下代码中选择“按名称搜索”选项时,单击“ID EditText”框视图。按ID搜索时,单击Name EditText框视图。以下是布局XML文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testingserver.SearchMember" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextName"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:text="Search" />
<EditText
android:id="@+id/editTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_centerHorizontal="true"
android:ems="10"
android:text="ID"
android:visibility="gone"
/>
<EditText
android:id="@+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_centerHorizontal="true"
android:ems="10"
android:text="Name"
android:visibility="gone"
/>
</RelativeLayout>
活动类功能如下。
package com.example.testingserver;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class SearchMember extends Activity implements AdapterView.OnItemSelectedListener{
Integer[] option_values= {0,1, 2, 3};
EditText editTextID;
EditText editTextName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_member);
editTextID= (EditText) findViewById(R.id.editTextID);
editTextName= (EditText) findViewById(R.id.editTextName);
String[] options= {"Select","Search By ID", "Search By Name", "Search By Phone"};
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, options);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_member, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//EditText editTextID= (EditText) findViewById(R.id.editTextID);
//EditText editTextName= (EditText) findViewById(R.id.editTextName);
int opt= position;
switch(opt){
case 1:
editTextID.setVisibility(View.VISIBLE);
editTextName.setVisibility(View.GONE);
break;
case 2:
editTextName.setVisibility(View.VISIBLE);
editTextID.setVisibility(View.GONE);
break;
default:
}
//Toast.makeText(getApplicationContext(), websites[position], Toast.LENGTH_LONG);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
当选择按ID搜索选项变量opt为1但名称编辑文本框变为可见时。当按名称搜索选项时,变量opt是bcome 2,ID编辑框可见。似乎switch语句工作正常。但结果不是我的预期。
答案 0 :(得分:-1)
请检查可能是您找到另一个名称的id表示带有id的edittextname和带有oncreate名称的edittextid。