简体   繁体   English

从扩展Activity的类中调用View

[英]Call a View from a class which extends Activity

I've got a java class that extends Activity (not AppCompatActivity!). 我有一个扩展Activity(不是AppCompatActivity!)的Java类。

Now it happens that I want to call a View which was defined in an .xml file. 现在碰巧我想调用一个在.xml文件中定义的View。
How do I do that? 我怎么做?
My app keeps crashing, but if I let the .java class extend AppCompatActivity, it doesn't. 我的应用程序一直崩溃,但是如果我让.java类扩展AppCompatActivity,则不会。

Any ideas on how ti fix that? 关于如何解决这个问题的任何想法?

Sadly it has to extend Activity. 可悲的是它必须扩展Activity。

The .java class .java类

public class pop extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int) (width * .8), (int) (height * .4)); // Width * 0,8 == 80% der Fenstergröße

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-view.
                parent.setBackgroundColor(Color.argb(250,0,0,0));
                finish();
            }
        });
    }

}

pop.java pop.java

public class pop extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup);

    }

    @Override
    protected void onResume() {
        super.onResume();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-view.
                parent.setBackgroundColor(Color.argb(250,0,0,0));
                finish();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

}

I have already found teh solution, thank you all foor the help! 我已经找到了解决方案,谢谢大家的帮助! I really appreciate that! 我真的很感激!

Here's one way (as mentioned in the comments) utilising onResume , it's all handled in the invoking activity basically by setting a variable to indicate that the pop activity has been started ( note as you may be using a dialog that clicking out side the dialog would have the same effect ). 这是一种利用onResume的方式(如注释中所述),基本上所有的操作都在调用活动中进行,基本上是通过设置一个变量来指示pop活动已经开始( 请注意,因为您可能正在使用一个对话框,请单击该对话框旁边的具有相同的效果 )。

This is pretty limited. 这是相当有限的。

public class MainActivity extends AppCompatActivity {

    boolean resumestate = false;
    LinearLayout ll; //The main layout

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.mainactivity_ll);

    }

    // Invoke the pop activity, setting resumestate to indicate this      
    public void doTestButton(View v) {
        Toast.makeText(this,"You Clicked the Test Button",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Pop.class);
        startActivity(intent);
        resumestate = true;
    }

    //Check if the activity was invoked and if so set background colour
    @Override
    protected void onResume() {
        super.onResume();
        if (resumestate) {
            ll.setBackgroundColor(Color.argb(250,0,0,0));
            resumestate = false;
        }
    }
}

Another way, using startActivityForresult and thus returning the background colour via a returned Intent (more flexible) :- 另一种方法,使用startActivityForresult并因此通过返回的Intent返回背景色(更灵活):

MainActivty 主活动

public class MainActivity extends AppCompatActivity {

    boolean resumestate = false;
    LinearLayout ll;
    int requestcode = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll = (LinearLayout) findViewById(R.id.mainactivity_ll);

    }

    public void doTestButton(View v) {
        Toast.makeText(this,"You Clicked the Test Button",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Pop.class);
        startActivityForResult(intent,requestcode);
        resumestate = true;
    }

    @Override
    protected void onActivityResult(int rqstcode, int resultcode, Intent data) {
        if (rqstcode == requestcode) {
            if (resultcode == Activity.RESULT_OK) {
                int newcolour = data.getIntExtra("BGRNDCOLOUR",Color.argb(250,48,48,48));
                ll.setBackgroundColor(newcolour);
            }
        }
    }
}

The button's onClick method in the pop activity :- pop活动中按钮的onClick方法:-

public void doTestButton(View v) {
    int newcolour = Color.argb(250,128,128,128);
    Intent ri = new Intent();
    ri.putExtra("BGRNDCOLOUR", newcolour);
    setResult(Activity.RESULT_OK,ri);
    finish();
}

A third way, which I think may be considered bad practice, would be to create a public static method to alter the background in conjunction with declaring the static variable for respective layout. 我认为可能被认为是不好的做法的第三种方法是创建一个public static方法来更改背景,同时声明相应布局的静态变量。

eg In the mainactivty replace LinearLayout ll; 例如在mainactivty中替换LinearLayout ll; with static LinearLayout ll; 带有static LinearLayout ll; and then add the appropriate method to main activity eg 然后将适当的方法添加到主要活动中,例如

    public static void alterBackGroundColour(int newcolour) {
        ll.setBackgroundColor(newcolour);
    }

then in the appropriate place in the pop activity use :- 然后在pop活动的适当位置使用:-

    MainActivity.alterBackGroundColour(Color.argb(250,128,0,128));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个将活动扩展到扩展视图的类的类访问变量 - Acces to variable from another a class that extends activity to a class that extends view 扩展Fragment的类的调用方法 - Call method from class which extends Fragment 调用从MainActivity类扩展Activity的Fragment扩展的类Fragment - Calling a Class Fragment which extends Fragment from a MainActivity Class which extends Activity 从扩展视图的类中调用活动类中的变量或方法? - Calling a variable or method in the activity class from a class that extends view? 如何从不扩展Activity,Android的类中创建DatePicker? - How to create a DatePicker from class which do not extends Activity, Android? 如何从不扩展 Activity、Android 的类创建 TimePicker? - How to create a TimePicker from class which do not extends Activity, Android? Android:无法从扩展Thread的类中调用新活动 - Android: Can't call new activity from class that extends Thread 关闭活动或从扩展ContextWrapper的类中调用finish() - Close activity or call finish() from class that extends ContextWrapper 从扩展应用程序的另一个 class 的活动中调用方法 - Call method in an activity from another class that extends Application 如何获取扩展片段/活动等的 class 的 class 的视图 - How to get the View for a class, that extends a class that extends Fragment/activity etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM