我现在正在学习Android,但遇到了问题。 我在导航抽屉项目的片段中使用了addTextChangedListener。文本从未更改,因为它应该根据EditText视图进行更改。我尝试调试,它表明编译器没有进入侦听器的3个子方法中的任何一个。有人知道发生了什么吗?
MainActivity.java:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Spinner fromSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
setTitle("ezConvert");
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.temperature:
TempFragment tempFragment = new TempFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout_main, tempFragment).commit();
break;
case R.id.length:
break;
case R.id.volume:
break;
case R.id.weight:
break;
case R.id.nav_share:
break;
case R.id.contact:
break;
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
TempFragment.java:
public class TempFragment extends Fragment implements AdapterView.OnItemSelectedListener {
private Spinner fromSpinner;
private Spinner toSpinner;
private ArrayAdapter adapter1;
private ArrayAdapter adapter2;
private View rootView;
private EditText input;
private TextView display;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_temp, container, false);
input = rootView.findViewById(R.id.fromValue);
display = rootView.findViewById(R.id.result);
fromSpinner = rootView.findViewById(R.id.convertFrom);
adapter1 = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.temp,android.R.layout.simple_spinner_dropdown_item);
fromSpinner.setAdapter(adapter1);
fromSpinner.setOnItemSelectedListener(this);
toSpinner = rootView.findViewById(R.id.convertTo);
adapter2 = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.temp,android.R.layout.simple_spinner_dropdown_item);
toSpinner.setAdapter(adapter2);
toSpinner.setOnItemSelectedListener(this);
return rootView;
}
public void convert(){
display.addTextChangedListener(new TextWatcher() {
// @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
// @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(fromSpinner.getSelectedItem().toString().equals("Celsius[℃]") && toSpinner.getSelectedItem().toString().equals("Fahrenheit[℉]")){
double inputNum = Double.valueOf(input.getText().toString());
double result = inputNum * 1.8 + 32;
display.setText(input.getText().toString() + " " + fromSpinner.getSelectedItem().toString() + " = " + result + " " + toSpinner.getSelectedItem().toString());
}
else if(fromSpinner.getSelectedItem().toString().equals(toSpinner.getSelectedItem().toString())){
display.setText(input.getText().toString() + " " + fromSpinner.getSelectedItem().toString() + " = " + input.getText().toString() + " " + toSpinner.getSelectedItem().toString());
}
}
// @Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
convert();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
fragemnt_temp.xml:
<TextView
android:id="@+id/fromTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:text="From: "
android:textSize="30dp"
android:height="40dp" />
<Spinner
android:id="@+id/convertFrom"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginEnd="40dp"
android:layout_toEndOf="@+id/fromTitle" />
<EditText
android:id="@+id/fromValue"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/fromTitle"
android:layout_marginLeft="36dp"
android:layout_marginRight="38dp"
android:ems="10"
android:hint="From"
android:inputType="number" />
<TextView
android:id="@+id/toTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_below="@+id/fromValue"
android:text="To: "
android:textSize="30dp"
android:height="40dp" />
<Spinner
android:id="@+id/convertTo"
android:layout_below="@+id/fromValue"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginEnd="40dp"
android:layout_toEndOf="@+id/toTitle" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_below="@+id/convertTo"
android:text="Result"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:height="40dp" />
我相信问题是因为调试器不断告诉我“没有这样的实例字段显示”。如果此问题已解决,则应解决。
答案 0 :(得分:0)
您的方法convert()
仅设置了addTextChangedListener,但并未真正更改任何文本,因此永远不会触发回调。
并且,如果您需要通过EditText更改TextView的文本,则应将侦听器添加到EditText。
试试:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_temp, container, false);
input = rootView.findViewById(R.id.fromValue);
display = rootView.findViewById(R.id.result);
fromSpinner = rootView.findViewById(R.id.convertFrom);
adapter1 = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.temp,android.R.layout.simple_spinner_dropdown_item);
fromSpinner.setAdapter(adapter1);
fromSpinner.setOnItemSelectedListener(this);
toSpinner = rootView.findViewById(R.id.convertTo);
adapter2 = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.temp,android.R.layout.simple_spinner_dropdown_item);
toSpinner.setAdapter(adapter2);
toSpinner.setOnItemSelectedListener(this);
input.addTextChangedListener(new TextWatcher() {
// @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
// @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
convert();
}
// @Override
public void afterTextChanged(Editable s) {
}
});
return rootView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
convert();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
private void convert() {
if(fromSpinner.getSelectedItem().toString().equals("Celsius[℃]") && toSpinner.getSelectedItem().toString().equals("Fahrenheit[℉]")){
double inputNum = Double.valueOf(input.getText().toString());
double result = inputNum * 1.8 + 32;
display.setText(input.getText().toString() + " " + fromSpinner.getSelectedItem().toString() + " = " + result + " " + toSpinner.getSelectedItem().toString());
}
else if(fromSpinner.getSelectedItem().toString().equals(toSpinner.getSelectedItem().toString())){
display.setText(input.getText().toString() + " " + fromSpinner.getSelectedItem().toString() + " = " + input.getText().toString() + " " + toSpinner.getSelectedItem().toString());
}
}