我在一个类中有很多方法,哪些元素引用1个XML布局文件。如何将这些方法放在额外的类中?
所以我想把这段代码作为例子:
public void recordSound(){
// abgerunderter Rahmen für den Medienplayer wird in einer TextView
// angezeigt
TextView soundUmrandung = (TextView) findViewById(R.id.soundumrandungtextview);
// das Design (abgerundeter Rahmen) für den Medienplayer laden
Resources res = getApplicationContext().getResources();
Drawable drawable = res.getDrawable(R.drawable.soundshape);
// das geladene Design auf der TextView anwenden, damit ein abgerunter
// Rahmen erscheint
soundUmrandung.setBackground(drawable);
ImageButton record = (ImageButton) findViewById(R.id.sound);
ImageButton recordStop = (ImageButton) findViewById(R.id.soundstop);
和
public void builtButtonsRigtLeftPic() {
ImageButton arrowLeft = (ImageButton) findViewById(R.id.arrowLeft);
ImageButton arrowRight = (ImageButton) findViewById(R.id.arrowRight);
arrowLeft.setVisibility(View.VISIBLE);
arrowRight.setVisibility(View.VISIBLE);
Resources res = getApplicationContext().getResources();
Drawable leftgrey = res.getDrawable(R.drawable.arrowleftgrey);
Drawable rightgrey = res.getDrawable(R.drawable.arrowrightgrey);
哪些元素将相同的layoutFile引用到不同的类中。
我用
尝试了LayoutInflater layoutinflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View openprojectView = layoutinflater.inflate(R.layout.openproject, null);
设置LayoutResource并使用intent调用新类(如Intent soundRecord = new Intent(this, SoundRecord.class);startActivity(soundRecord);
,但这导致错误:
07-20 21:19:00.590: E/AndroidRuntime(20296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unitnode/com.unitnode.SoundRecord}: android.view.InflateException: Binary XML file line #88: Error inflating class fragment
现在我在新课程中尝试过这种方式:
@覆盖 protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.openproject);
LayoutInflater layoutinflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
openprojectView = layoutinflater.inflate(R.layout.openproject, null);
//TextView sound = (TextView) openprojectView.findViewById(R.id.soundumrandungtextview);
//Log.d("openprojectView", "openprojectView " + openprojectView + "sound " + sound);
recordSound();
}
并参考布局:TextView soundUmrandung = (TextView) openprojectView.findViewById(R.id.soundumrandungtextview);
。结果不再是错误,但它显示一个新的空白视图。当我关闭新的空白视图时,我看到除Buttons之外的所有内容都丢失了clickListener。所以我不能再录制音频了。
当我尝试用new SoundRecord(context)
打开新类时,按钮也丢失了ClickListener。当我从Activity扩展类时,对我来说似乎是错误的,因为类不应该打开一个新的Activity。 Class应该只能访问父活动的元素。
现在我的班级:
公共类SoundRecord {
private SharedPreferences sharedPreferences;
private MediaRecorder mediaRecorder = null;
private File soundFile = null;
private String soundFilePath;
private View openprojectView;
//final Context context = this;
public SoundRecord(Context context){
LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
//setContentView(R.layout.openproject);
//LayoutInflater layoutinflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
openprojectView = layoutinflater.inflate(R.layout.openproject, null);
//TextView sound = (TextView) openprojectView.findViewById(R.id.soundumrandungtextview);
//Log.d("openprojectView", "openprojectView " + openprojectView + "sound " + sound);
recordSound(context);
}
在OpenProject中,该类由:new SoundRecord(context);
请帮帮我。我真的需要将我的代码分成几类。