应用程序启动时弹出菜单

时间:2017-03-06 18:45:16

标签: android xml

我有一个应用程序可以选择在开始时选择语言。目前,当应用程序启动时,我必须单击选择语言,然后弹出显示包含不同的语言。但是我不想点击选择语言选项来显示弹出窗口。我希望我的应用程序自动出现一个弹出窗口,以便在应用启动时选择语言。我跟着 this 教程。这是我想要实现的事情的快照。

enter image description here

以下是Mainactivity.java的代码

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (pos == 1) {
                    Toast.makeText(parent.getContext(), "You have selected Tamil", Toast.LENGTH_SHORT).show();
                    setLocale("ta");
                } else if (pos == 2) {
                    Toast.makeText(parent.getContext(), "You have selected Hindi", Toast.LENGTH_SHORT).show();
                    setLocale("hi");
                } else if (pos == 3) {
                    Toast.makeText(parent.getContext(), "You have selected English", Toast.LENGTH_SHORT).show();
                    setLocale("en");
                }
                else if (pos == 4) {
                    Toast.makeText(parent.getContext(), "You have selected Arabic", Toast.LENGTH_SHORT).show();
                    setLocale("ar");
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
    }

    public void setLocale(String lang) {
        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}

我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/greet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/greet"
        android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sachin" android:paddingTop="25sp" />

    <TextView
        android:textColor="#ffffff"
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/langselection"
        android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>

    <Spinner
        android:popupBackground="#ff004372"
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/languages"
        android:gravity="center" 
        android:paddingTop="25sp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

onCreate()的{​​{1}}中尝试此操作:

Activity

它会在spinnerctrl = (Spinner) findViewById(R.id.spinner1); findViewById(android.R.id.content).post(new Runnable() { @Override public void run() { spinnerctrl.performClick(); } }); //Rest of the code... 的onCreate期间自动打开spinner弹出窗口

<强>更新

取消可运行的使用:

宣告:

Activity

Handler handler; Runnable runnable;

onCreate()

现在使用:

handler = new Handler(); runnable = new Runnable() { public void run() { spinnerctrl.performClick(); } }; handler.postDelayed(this, 500); 取消它
onItemSelected