我的应用包含一个表单,如下图所示:
当我点击菜单选项按钮时,抽屉将打开,如下图所示:
我希望在按下选择位置按钮时打开抽屉。
我的代码是
RegisterBlood.java
...
public class RegisterBlood extends Activity implements OnItemClickListener {
DrawerLayout dLayout, dLayout2;
ListView dList, dList2;
ArrayAdapter<String> adapter, adapter2;
String[] state_data2;
String city = "", state = "";
Button location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocation);
Bundle args = new Bundle();
Fragment detail = new RegisterBloodFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail)
.commit();
...
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, state_data);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
// dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
dList.setOnItemClickListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// your action...
if (!dLayout.isDrawerOpen(dList)) {
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, state_data2);
dList.setAdapter(adapter);
dLayout.openDrawer(dList);
dList.setOnItemClickListener(this);
}
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your action...
if (dLayout.isDrawerOpen(dList)) {
dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
return true;
}
return super.onKeyDown(keyCode, e);
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, final int position,
long id) {
// TODO Auto-generated method stub
...
dLayout2 = (DrawerLayout) findViewById(R.id.drawer_layout);
dList2 = (ListView) findViewById(R.id.left_drawer);
adapter2 = new ArrayAdapter<String>(RegisterBlood.this,
android.R.layout.simple_list_item_1, city_data);
dList2.setAdapter(adapter2);
dList2.setSelector(android.R.color.holo_blue_dark);
dLayout2.openDrawer(dList2);
dLayout2.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
dList2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position2,
long id) {
dLayout2.closeDrawers();
state = state_data2[position];
city = city_data[position2];
Bundle args = new Bundle();
args.putString("Menu", city_data[position2] + " ("
+ state_data2[position] + ")");
Fragment detail = new RegisterBloodFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
}
});
}}
RegisterBloodFragment.java
...
public class RegisterBloodFragment extends Fragment implements OnClickListener {
Button location;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.registerblood, container, false);
String menu = getArguments().getString("Menu");
location= (Button) view.findViewById(R.id.etlocation);
location.setText(menu);
//Context c=getActivity();
//location.setOnClickListener(c.getApplicationContext().set);
return view;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),
"Yes",
Toast.LENGTH_SHORT).show();
}
}
registerblood.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/silver"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="10dp" >
<EditText
android:id="@+id/bregetName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="Name" />
<EditText
android:id="@+id/bregetBloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="Blood Group" />
<Button
android:id="@+id/etlocation"
style="@android:style/Widget.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="select location" >
</Button>
<Button
android:id="@+id/bregbtnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:background="@drawable/button"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Submit"
android:textColor="#FFFFFF"
android:textSize="24sp" />
</LinearLayout>
selectlocation.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/>
我的问题是:
如何为onclicklistener
中的选择位置按钮添加RegisterBlood.java
或从onClickListener
拨打RegisterBlood.java
中的RegisterBloodFragment.java
?
请帮帮我。
答案 0 :(得分:15)
由于按钮是片段布局的一部分,因此将侦听器设置为:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.registerblood, container, false);
String menu = getArguments().getString("Menu");
location = (Button) view.findViewById(R.id.etlocation);
location.setText(menu);
location.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
RegisterBlood activity = (RegisterBlood) getActivity();
// Now you can contact your activity through activity e.g.:
activity.onKeyDown(KeyEvent.KEYCODE_MENU, null);
}
答案 1 :(得分:9)
首先记得实现OnClickListener接口:
public class YourClassName extends Fragment implements View.OnClickListener
public Button button;
然后,在方法OnCreateView中,膨胀按钮并将侦听器设置为它,如下所示:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
button = (Button) inflater.inflate(R.layout.registerblood, container, false).findViewById(R.id.your_button_id);
button.setOnClickListener(this);
}
然后@Override
函数onClick
并做任何你要做的事情:
@Override
public void onClick(View v) {
//YOUR CODE HERE
}
EDIT
如果您只是在寻找FocusListener
(在这种情况下,您的问题需要更新),请在onCreateView
方法上设置监听器:
your_edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
// OPEN THE DRAWER HERE
} else {
Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show();
}
}
或者您可以将该听众实施到您的班级。
答案 2 :(得分:0)
你需要像这样为按钮点击
充气片段 View view = inflater.inflate(R.layout.fragment_blank3,
container, false);
Button bt1=(Button)view.findViewbyId(R.id.buttton);
bt1.setOnclick...
//then return in on create view.
return view;