我是新的android / java学生 我尝试制作测试应用程序 但是我遇到了问题并且搜索了它但没有解决 我创建了一个新的xml文件(应用程序的新屏幕),我想在新的java类中使用xml元素。
搜索后尝试使用Inflator但没有工作 没有错误但没有返回所需的值。 请帮忙。
* Main Class.java
package online.pharmastars.dosecalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.drug_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.drugs_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
String selectedDrug = "";
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
selectedDrug = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
public void calculateDose(View view){
Intent doseCalculate = new Intent(this, DoseCalculation.class);
startActivity(doseCalculate);
}
}
新的class.java是:
package online.pharmastars.dosecalculator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static online.pharmastars.dosecalculator.R.id.weight;
/**
* Created by ahmed on 4/24/2017.
*/
public class DoseCalculation extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dose_calculation);
calculateDose();
}
public void calculateDose(){
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.activity_main, null);
EditText pWeight = (EditText) view.findViewById(R.id.age_years);
String test = pWeight.getText().toString();
TextView patientWeight = (TextView) findViewById(R.id.text_view_weight);
patientWeight.setText(test);
}
}
答案 0 :(得分:0)
我想您忘记在onCreate方法中打开新方法的方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.drug_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.drugs_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
calculateDose();
}
并像这样修改您的方法
public void calculateDose(View view){
Intent doseCalculate = new Intent(this, DoseCalculation.class);
startActivity(doseCalculate);
}
并且在calculateDose方法中,不需要参数View