重新发布这个,因为我最初用错误的标题发布它,所以它在没有问题的情况下迷失了。
我正在创建一个小型时间表应用,目前正在创建一个带有一些标签的屏幕。每个选项卡都是一个单独的片段,每个选项卡中都有20个编辑文本。我的目标是,如果用户键入其中一个编辑文本,然后单击它并且焦点丢失,则将文本从编辑文本字段发送到主活动。然后,单击确认按钮时,编辑文本中的任何文本都将保存到SQLite数据库中。 我的问题是焦点听众似乎没有工作。 这是片段代码之一:
package com.example.schoolandrevisiontimetable;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
public class Input_slessontime_monday extends Fragment implements OnFocusChangeListener{
OnMondayEditTextChangedListener monCallback;
private String mondayString;
private int mondayPeriod;
private int mondayPosition;
public void MondayEditTextChanged(String mondayEditText, int period, int position){
mondayString = mondayEditText;
mondayPeriod = period;
mondayPosition=position;
monCallback.onMondayEditTextChanged(mondayString, mondayPeriod, mondayPosition);
}
public interface OnMondayEditTextChangedListener {
public void onMondayEditTextChanged(String mondayEditText, int period, int position);
}
EditText[][] mondayInput = new EditText[11][2];
String [][] mondayStrings = new String [11][2];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_input_slessontime_monday,
container, false);
mondayInput[1][0] = (EditText) rootView.findViewById(R.id.mondayslesson1start);
mondayInput[1][1] = (EditText) rootView.findViewById(R.id.mondayslesson1end);
mondayInput[2][0] = (EditText) rootView.findViewById(R.id.mondayslesson2start);
mondayInput[2][1] = (EditText) rootView.findViewById(R.id.mondayslesson2end);
...
//this continues up to 10. Had to delete the rest due to character limitations
Log.v("Monday", "Edit texts all set");
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
monCallback = (OnMondayEditTextChangedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnMondayEditTextChanged");
}
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.v("monday", "focus change!");
String edittext;
if(!hasFocus)
switch(v.getId()){
case R.id.mondayslesson1start:
edittext = mondayInput[1][0].getText().toString();
MondayEditTextChanged(edittext,1,0);
break;
case R.id.mondayslesson1end:
edittext = mondayInput[1][1].getText().toString();
MondayEditTextChanged(edittext,1,1);
break;
case R.id.mondayslesson2start:
edittext = mondayInput[2][0].getText().toString();
MondayEditTextChanged(edittext,2,0);
break;
case R.id.mondayslesson2end:
edittext = mondayInput[2][1].getText().toString();
MondayEditTextChanged(edittext,2,1);
break;
//this continues up to 10. Had to delete the rest due to character limitations
}
}
}
以下是活动的代码:
package com.example.schoolandrevisiontimetable;
import java.util.List;
import com.astuetz.PagerSlidingTabStrip;
import Database.DatabaseHelper;
import Database.MC_sschoolday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
/**DETECTIVEREPORT:
* onfocuschange does nothing
* but it seems that getCount() is called everytime you click onto another textbox, or scroll. probably unrelated.
* **/
public class Input_slessontime extends FragmentActivity implements
Input_slessontime_monday.OnMondayEditTextChangedListener,
Input_slessontime_tuesday.OnTuesdayEditTextChangedListener,
Input_slessontime_wednesday.OnWednesdayEditTextChangedListener{
//this class uses the pagerslidingtabstrip library from https://github.com/astuetz/PagerSlidingTabStrip to create a tab layout.
MC_sschoolday[] sday = new MC_sschoolday[7]; //new array of schooldays
int amountOfDays;
List<MC_sschoolday> schoolday;
DatabaseHelper db;
public static FragmentManager fgmanger;
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
Input_slessontime_monday mondayFragment;
String[][][] slessonTimes = new String[8][11][2];
int numberOfLessonTimes=0;
String[] slessonPointer = new String[140];
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("LessonTimeInput", "oncreate");
db = new DatabaseHelper(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_slessontime);
fgmanger = getSupportFragmentManager();
schoolday = db.getAllSSchoolDays();
amountOfDays = getAmountOfDaysUsed();
tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs); //set the tabs
pager = (ViewPager) findViewById(R.id.pager); //set the pager
adapter = new MyPagerAdapter(fgmanger); //set the adapter
pager.setAdapter(adapter); //set the adapter to the pager
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
db.close();
}
@Override
public void onMondayEditTextChanged(String mondayEditText, int period, int position) {
slessonTimes[1][period][position] = mondayEditText;
Log.v("LessonTimeInput", "MondayLessontime Added "+ period + " " + position);
slessonPointer[numberOfLessonTimes] = ("1"+period+position);
numberOfLessonTimes++;
}
@Override
public void onTuesdayEditTextChanged(String tuesdayEditText, int period,
int position) {
Log.v("LessonTimeInput", "TuesdayLessontime Added "+ period + " " + position);
slessonTimes[2][period][position] = tuesdayEditText;
slessonPointer[numberOfLessonTimes] = ("2"+period+position);
numberOfLessonTimes++;
}
@Override
public void onWednesdayEditTextChanged(String wednesdayEditText, int period,
int position) {
Log.v("LessonTimeInput", "TuesdayLessontime Added "+ period + " " + position);
slessonTimes[3][period][position] = wednesdayEditText;
slessonPointer[numberOfLessonTimes] = ("3"+period+position);
numberOfLessonTimes++;
}
public void confirmslesson(View v){
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.input_slessontime, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
//this gets the amount of days used, so that the code knows how many tabs to generate
public int getAmountOfDaysUsed(){
db = new DatabaseHelper(getApplicationContext()); //open the database
int amountOfDaysUsed=0; //initialise this int
for (int i =0; i<7; i++){
sday[i]=schoolday.get(i); //stores the lessons in an array
}
for (int i =0; i<7; i++){
if(sday[i].getUsed().toString().equals("y")){ //find how many days are used
amountOfDaysUsed++;
}
}
db.close(); //close the database
Log.v("LessonTimeInput", "amount of days used " + amountOfDaysUsed);
return amountOfDaysUsed; //return the int
}
//small subclass for the pageradapter
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
String[] days = new String[7];
db = new DatabaseHelper(getApplicationContext());
int usedCount2=0;
for (int i =0; i< 7; i++){
sday[i]=schoolday.get(i);
if(sday[i].getUsed().toString().equals("y")){
days[usedCount2] = sday[i].getschool_day();
usedCount2++;
}
}
db.close();
Log.v("LessonTimeInput", "getPageTitle");
return days[position];
}
@Override
public int getCount() {
Log.v("LessonTimeInput", "getCount");
return getAmountOfDaysUsed();
}
public Fragment switchFragmentDay(int num) {
DatabaseHelper db;
db = new DatabaseHelper(getApplicationContext());
MC_sschoolday[] sday = new MC_sschoolday[7];
int usedCount1=amountOfDays;
int [] dayID = new int[usedCount1];
int usedCount2=0;
for (int i =0; i< amountOfDays; i++){
sday[i]=schoolday.get(i);
if(sday[i].getUsed().toString().equals("y")){
dayID[usedCount2] = (int) sday[i].getSchool_day_id();
usedCount2++;
}
}
db.close();
switch (dayID[num]){
case 1:
return new Input_slessontime_monday();
case 2:
return new Input_slessontime_tuesday();
case 3:
return new Input_slessontime_wednesday();
case 4:
return new Input_slessontime_thursday();
case 5:
return new Input_slessontime_friday();
case 6:
return new Input_slessontime_saturday();
case 7:
return new Input_slessontime_sunday();
}
return null;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return switchFragmentDay(0);
case 1:
return switchFragmentDay(1);
case 2:
return switchFragmentDay(2);
case 3:
return switchFragmentDay(3);
case 4:
return switchFragmentDay(4);
case 5:
return switchFragmentDay(5);
case 6:
return switchFragmentDay(6);
}
return null;
}
}
public void StartIntent(Class<?> intentclass) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, intentclass);
startActivity(intent);
}
}
片段的XML:
<ScrollView
android:id="@+id/monday_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.pipturner.timetable.Input_slessontime_monday"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson1start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson1end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson2start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson1used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson2end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson2used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson3start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson3end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson3used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson4start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson4end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson4used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson5start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson5end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson5used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson6start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson6end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson6used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson7start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson7end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson7used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson8start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson8end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson8used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson9start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson9end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson9used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="@string/prompt_lesson" />
<EditText
android:id="@+id/mondayslesson10start"
android:inputType="textMultiLine"
android:hint="@string/start"
android:dependency="mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
<EditText
android:id="@+id/mondayslesson10end"
android:inputType="textMultiLine"
android:hint="@string/end"
android:dependency="mondayslesson10used"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10" />
</LinearLayout>
</LinearLayout>
</ScrollView>
以及活动的XML:
<LinearLayout 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:orientation="vertical" >
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:context=".Input_slessontime" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/cancelslessontime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:layout_weight="1" />
<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/confirmslessontime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/confirm"
android:layout_weight="1"
android:onClick="confirmslesson" />
</LinearLayout>
</LinearLayout>
可能与之相关的是,当我单击或关闭编辑文本或滚动片段时,会调用getCount()。可能不相关。
提前致谢!
答案 0 :(得分:0)
需要设置onFocusListener。 //感谢帮助人// ¬¬