【问题标题】:Popup Window in Android with buttons and text fieldsAndroid中带有按钮和文本字段的弹出窗口
【发布时间】:2014-11-17 22:28:51
【问题描述】:

想知道我的代码有什么问题。弹出窗口显示简单文本效果很好,但是当我尝试添加复杂的东西时,比如两个按钮和三个编辑字段就不起作用了。我的布局在 requestPane 线性布局中,当我实现 setContentView(layoutRequestPane) 之类的东西时,它会阻塞并退出应用程序。 如何在弹出窗口中显示活动按钮和活动字段等内容?

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

PopupWindow popUp;
LinearLayout layout;
TextView tv;
EditText textEdit;
LayoutParams params;
LinearLayout mainLayout, layoutRequestPane;
Button but;
boolean click = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    popUp = new PopupWindow(this);
    layout = new LinearLayout(this);
    layoutRequestPane=new LinearLayout(this);
    mainLayout = new LinearLayout(this);
    tv = new TextView(this);
    textEdit=new EditText(this);
    but = new Button(this);
    but.setText("Click Me");

    setContentView(R.layout.activity_main);

    but.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (click) {
                popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 200, 50);
                popUp.update(0, 50, 900, 80); 
                click = false;
            } else {
                popUp.dismiss();
                click = true;
            }
        }

    });

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    layoutRequestPane=(LinearLayout)findViewById(R.id.requestPaneLayout);
    layout.setOrientation(LinearLayout.VERTICAL);
    tv.setText("Hi this is a sample text for popup window");
    textEdit.getText();
    layout.addView(tv, params);
    popUp.setContentView(layout);
    // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
    mainLayout.addView(but, params);
    setContentView(mainLayout);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

【问题讨论】:

    标签: android button textfield popupwindow


    【解决方案1】:

    您似乎是 Android 的初学者(如果不是 - 我很抱歉),所以我建议您做一些事情:

    1 - 好好看看一些关于布局和活动等的教程。

    2 - 使用 xml 构建您的第一个 UI。当您对它的工作原理有了很好的了解后,您就可以开始以编程方式创建/处理布局和视图

    此外 - 乍一看:

    1 - 你用 (LinearLayout)findViewById(R.id.requestPaneLayout) 覆盖 layoutRequestPane - 确保你的布局不为空,并且确实是你所期望的。

    2 - 你调用 setContentView 两次。 很多不必要的事情发生。试着在那里整理一下。

    【讨论】:

    • 橘子,感谢您的回复。我是初学者,这是真的,但我从朋友那里得到建议,从一些特定的项目开始,从网上获取一些类似的代码并尝试调整它。他说这应该是最好的学习方式。看书和写书的例子用处不大。
    • 好的教程总是包含对你有帮助的代码。例如,看看 Vogella。此外 - 正如我所说,使用 xmls 来构建你的第一个 UI。在这里,您尝试以编程方式膨胀和添加它们,这不太简单,而且 - imo - 在编写您的第一个应用程序时并不理想。
    • 优秀的网站 Tangerine,thanx... 希望能找到我需要的东西。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多