之前我已经制作了这个应用程序。它运行得很好。 但现在我想用滑动视图来制作它。 现在我遇到了这些问题。
这是我的片段类
public class currencyclass extends Fragment {
public View onCreateView (LayoutInflater inflater, ViewGroup container , Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.currency , container , false);
return rootView;
//我有"无法访问的声明"仅此错误" editmeter"线
final EditText editmeter = (EditText) getView().findViewById(R.id.editMeter);
final EditText editinch = (EditText) getView().findViewById(R.id.editInch);
final EditText editcentimeter = (EditText) getView().findViewById(R.id.editCentimeter);
final EditText edityard = (EditText) getView().findViewById(R.id.editYard);
final EditText editfeet = (EditText) getView().findViewById(R.id.editFeet);
TextView textmeter = (TextView) getView().findViewById(R.id.textMeter);
TextView textinch = (TextView) getView().findViewById(R.id.textInch);
TextView textcentimeter = (TextView) getView().findViewById(R.id.textCentimeter);
TextView textyard = (TextView) getView().findViewById(R.id.textYard);
TextView textfeet = (TextView) getView().findViewById(R.id.textFeet);
TextView button = (TextView) getView().findViewById(R.id.buttonConvert);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "CoffeeHouse.ttf");
textmeter.setTypeface(font);
textinch.setTypeface(font);
textcentimeter.setTypeface(font);
textyard.setTypeface(font);
textfeet.setTypeface(font);
button.setTypeface(font);
editmeter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Button buttonconvert = (Button) getView().findViewById(R.id.buttonConvert);
buttonconvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double meter = Double.valueOf(editmeter.getText().toString());
double inch = meter * 39.3701;
editinch.setText(String.valueOf(inch));
double centimeter = meter * 10.0;
editcentimeter.setText(String.valueOf(centimeter));
double yard = meter * 1.0936;
edityard.setText(String.valueOf(yard));
double feet = meter * 3.2808;
editfeet.setText(String.valueOf(feet));
}
}
);
}else{
}
}
});
editinch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Button buttonconvert = (Button) getView().findViewById(R.id.buttonConvert);
buttonconvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double inch2 = Double.valueOf(editinch.getText().toString());
double meter2 = inch2 * 0.0254;
editmeter.setText(String.valueOf(meter2));
double centimeter2 = inch2 * 2.54;
editcentimeter.setText(String.valueOf(centimeter2));
double yard2 = inch2 * 0.0277778;
edityard.setText(String.valueOf(yard2));
double feet2 = inch2 * 0.0833333;
editfeet.setText(String.valueOf(feet2));
}
}
);
}else{
}
}
});
editcentimeter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Button buttonconvert = (Button) getView().findViewById(R.id.buttonConvert);
buttonconvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double centimeter3 = Double.valueOf(editcentimeter.getText().toString());
double meter3 = centimeter3 * 0.01;
editmeter.setText(String.valueOf(meter3));
double inch3 = centimeter3 * 0.393701;
editinch.setText(String.valueOf(inch3));
double yard3 = centimeter3 * 0.0109361;
edityard.setText(String.valueOf(yard3));
double feet3 = centimeter3 * 0.0328084;
editfeet.setText(String.valueOf(feet3));
}
}
);
}else{
}
}
});
edityard.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Button buttonconvert = (Button) getView().findViewById(R.id.buttonConvert);
buttonconvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double yard4 = Double.valueOf(edityard.getText().toString());
double meter4 = yard4 * 0.9144;
editmeter.setText(String.valueOf(meter4));
double centimeter4 = yard4 * 91.44;
editcentimeter.setText(String.valueOf(centimeter4));
double inch4 = yard4 * 36;
editinch.setText(String.valueOf(inch4));
double feet4 = yard4 * 3;
editfeet.setText(String.valueOf(feet4));
}
}
);
}else{
}
}
});
editfeet.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Button buttonconvert = (Button) getView().findViewById(R.id.buttonConvert);
buttonconvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double feet5 = Double.valueOf(editfeet.getText().toString());
double meter5 = feet5 * 0.3048;
editmeter.setText(String.valueOf(meter5));
double centimeter5 = feet5 * 30.48;
editcentimeter.setText(String.valueOf(centimeter5));
double yard5 = feet5 * 0.333333;
edityard.setText(String.valueOf(yard5));
double inch5 = feet5 * 12;
editinch.setText(String.valueOf(inch5));
}
}
);
}else{
}
}
});
}
}
答案 0 :(得分:3)
return关键字表示方法的最后一行。它会强制您的方法返回rootView的值,下面的所有行都将永远不会到达。 这就是为什么return语句应该是方法的最后一个语句,除非它在条件块中。