下面列出了android.widget.EditText#setId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mEditText = new EditText(context, attrs);
// Give it an ID so it can be saved/restored
mEditText.setId(com.android.internal.R.id.edit);
/*
* The preference framework and view framework both have an 'enabled'
* attribute. Most likely, the 'enabled' specified in this XML is for
* the preference framework, but it was also given to the view framework.
* We reset the enabled state.
*/
mEditText.setEnabled(true);
}
public static void getInput(Context context, String title, DialogInterface.OnClickListener onOkListener)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setId(R.id.text1);
builder.setView(input);
builder.setPositiveButton("OK", onOkListener);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
/**
* Adds the new edit to the list with id, text and hint
**/
public DialogBuilder addEdit(int id, String text, String hint) {
// inflate row using default layout
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.row_edit_dialog, null);
// if there are some rows above
if (listLayout.getChildCount() > 0) {
// show top border
View borderView = itemView.findViewById(R.id.item_top_border);
if (borderView != null) {
borderView.setVisibility(View.VISIBLE);
}
}
// setup edit
EditText editText = (EditText) itemView.findViewById(R.id.edit_text);
editText.setText(text);
editText.setSelection(editText.getText().length());
editText.setHint(hint);
editText.setId(id);
return addItem(itemView);
}
private void saveAsDialog(String path,
DialogInterface.OnClickListener listener)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.save);
builder.setMessage(R.string.choose);
// Add the buttons
builder.setPositiveButton(R.string.save, listener);
builder.setNegativeButton(R.string.cancel, listener);
// Create edit text
Context context = builder.getContext();
EditText text = new EditText(context);
text.setId(R.id.path_text);
text.setText(path);
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.setView(text, 40, 0, 40, 0);
dialog.show();
}
private void updateDialog(int title, String value, int hint,
DialogInterface.OnClickListener listener)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
// Add the buttons
builder.setPositiveButton(R.string.ok, listener);
builder.setNegativeButton(R.string.cancel, listener);
// Create edit text
Context context = builder.getContext();
EditText text = new EditText(context);
text.setId(R.id.value);
text.setText(value);
text.setHint(hint);
text.setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL);
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.setView(text, 40, 0, 40, 0);
dialog.show();
}
/**
* Generate a PinBox {@link EditText} with all attributes to add to {@link PinView}
*
* @param i index of new PinBox
* @param inputType inputType to new PinBox
* @return new PinBox
*/
EditText generatePinBox(int i, int inputType) {
EditText editText = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.partial_pin_box, this, false);
int generateViewId = PinViewUtils.generateViewId();
editText.setId(generateViewId);
editText.setTag(i);
if (inputType != -1) {
editText.setInputType(inputType);
}
setStylePinBox(editText);
editText.addTextChangedListener(this);
editText.setOnFocusChangeListener(this);
pinBoxesIds[i] = generateViewId;
return editText;
}
/**
* Generate a PinBox {@link EditText} with all attributes to add to {@link PinView}
*
* @param i index of new PinBox
* @param inputType inputType to new PinBox
* @return new PinBox
*/
EditText generatePinBox(int i, int inputType) {
EditText editText = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.partial_pin_box, this, false);
int generateViewId = PinViewUtils.generateViewId();
editText.setId(generateViewId);
editText.setTag(i);
if (inputType != -1) {
editText.setInputType(inputType);
}
setStylePinBox(editText);
editText.addTextChangedListener(this);
editText.setOnFocusChangeListener(this);
pinBoxesIds[i] = generateViewId;
return editText;
}
/**
* @inheritDoc Constructs an EditText object with simple text input
*/
@Override
public View getView(Context context) {
//Create row
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout row = new LinearLayout(context);
row.setOrientation(LinearLayout.HORIZONTAL);
//Create TextView
TextView t = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
t.setText(name);
row.addView(t);
//Create EditText
EditText text = new EditText(context);
text.setId(id);
row.addView(text);
view = row;
return view;
}
/**
* @inheritDoc Constructs an EditText object with input type set to number
*/
@Override
public View getView(Context context) {
//Create row
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout row = new LinearLayout(context);
row.setOrientation(LinearLayout.HORIZONTAL);
//Create TextView
TextView t = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
t.setText(name);
row.addView(t);
//Create EditText
EditText text = new EditText(context);
text.setInputType(InputType.TYPE_CLASS_NUMBER);
text.setId(id);
row.addView(text);
view = row;
return view;
}
private void showNewTweetDialog() {
final EditText tweetText = new EditText(this);
tweetText.setId(R.id.tweet_text);
tweetText.setSingleLine();
tweetText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
tweetText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(140)});
tweetText.setImeOptions(EditorInfo.IME_ACTION_DONE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.label_what_is_happening);
builder.setPositiveButton(R.string.action_tweet, (dialog, which) -> presenter.tweet(tweetText.getText().toString()));
AlertDialog alert = builder.create();
alert.setView(tweetText, 64, 0, 64, 0);
alert.show();
tweetText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
alert.getButton(DialogInterface.BUTTON_POSITIVE).callOnClick();
return true;
}
return false;
});
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setMessage(getString(R.string.enter_new_file_name));
// Set an EditText view to get user input
final String filename = getArguments().getString(ARG_FILENAME);
final EditText input = new EditText(getActivity());
input.setId(android.R.id.edit);
input.setSingleLine();
input.setText(filename);
StringPathUtil spu = new StringPathUtil(filename);
String fnWoExt = spu.getFileNameWithoutExtension();
if(fnWoExt.length() > 0)
input.setSelection(0, fnWoExt.length());
alert.setView(input);
alert.setPositiveButton(getString(android.R.string.ok),
(dialog, whichButton) -> renameFile(input.getText().toString()));
alert.setNegativeButton(android.R.string.cancel,
(dialog, whichButton) ->
{
// Canceled.
});
return alert.create();
}
/**
* Class Constructor.
*
* @param id
* the id used to uniquely identify this object.
* @param dataTypeDbID
* the database id for {@link OmniArea}
* @param activity
* the activity where this view item is to be built on
*/
public AreaViewItem(int id, long dataTypeDbID, Activity activity) {
super(id, dataTypeDbID);
mActivity = activity;
etAddress = new EditText(activity);
etAddress.setId(ADDRESS_VIEW_ID);
etDistance = new EditText(activity);
etDistance.setId(DISTANCE_VIEW_ID);
}
@Before
public void setup() {
EditText editText = new EditText(activityTestRule.getActivity());
editText.setId(editTextId);
editText.setHint(HINT_TEXT);
addViewToLayout(editText, BaseActivity.rootLayout);
}
public void showalert() {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.context);
alert.setTitle(text);
// Set an EditText view to get user input
final EditText input = new EditText(MainActivity.context);
input.setId(1);
if(src==false){
alert.setMessage("path to install");
input.setText(getPath());
}
else{
alert.setMessage("source file");
input.setText(getSource());
}
alert.setView(input);
alert.setPositiveButton("save", this);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
private AlertDialog createImportFromClipboardCodeDialog() {
final EditText view = new EditText(this);
view.setId(20130805);
return new AlertDialog.Builder(this).setTitle(R.string.script_import_from_clipboard_code).
setView(view).
setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogI, int button) {
importFromClipboard(view.getText().toString());
}
}).
setNegativeButton(android.R.string.cancel, null).
create();
}
private void givenEditTextToOpenSoftKeyboard() {
EditText editText = new EditText(activityTestRule.getActivity());
editText.setId(editTextId);
addViewToLayout(editText, BaseActivity.rootLayout);
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
editText = new EditText(this);
editText.setId(android.R.id.primary);
setContentView(editText);
}
private void setupView(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes){
_context = context;
RelativeLayout relativeLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
EditText editText = new EditText(context,attrs);
editText.setId(EDIT_TEXT_ID);
//
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RichEditText);
mRichEditEnabled = a.getBoolean(R.styleable.RichEditText_richEditAble,true);
a.recycle();
mImageButton = new ImageButton(context);
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams editToggleParams = new RelativeLayout.LayoutParams(px,px);
mImageButton.setBackground(getResources().getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp));
editToggleParams.addRule(RelativeLayout.ALIGN_BOTTOM, EDIT_TEXT_ID);
editToggleParams.addRule(RelativeLayout.ALIGN_RIGHT, EDIT_TEXT_ID);
mImageButton.setLayoutParams(editToggleParams);
mImageButton.setId(EDIT_TOGGLE_ID);
mImageButton.setRotation(-90);
mImageButton.setOnClickListener(this);
View htmlOptions = inflate(context,R.layout.htmloptions,null);
RelativeLayout.LayoutParams htmlOptionsLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
htmlOptionsLayoutParams.addRule(RelativeLayout.BELOW, 1001);
htmlOptionsLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT, 1001);
htmlOptions.setLayoutParams(htmlOptionsLayoutParams);
relativeLayout.setLayoutParams(params);
relativeLayout.addView(editText);
relativeLayout.addView(mImageButton);
//htmlOptions.setVisibility(View.GONE);
if(mRichEditEnabled) {
relativeLayout.addView(htmlOptions);
}
addView(relativeLayout);
this.mEditText = editText;
if(mRichEditEnabled) {
findViewById(R.id.makeBold).setOnClickListener(this);
findViewById(R.id.makeItalic).setOnClickListener(this);
findViewById(R.id.makeUnderline).setOnClickListener(this);
findViewById(R.id.makeBackground).setOnClickListener(this);
findViewById(R.id.makeForeground).setOnClickListener(this);
findViewById(R.id.makeHyperlink).setOnClickListener(this);
findViewById(R.id.makeStrikethrough).setOnClickListener(this);
findViewById(R.id.makeScaleX).setOnClickListener(this);
mHtmloptions = (LinearLayout) findViewById(R.id.rich_toolbar);
mHtmloptions.setVisibility(View.GONE);
// mImageButton = (ImageButton) findViewById(R.id.list_toggle);
// mImageButton.setOnClickListener(this);
}
this.mEditText.setOnClickListener(this);
setOnClickListener(this);
mSS = new SpannableStringBuilder(mEditText.getText());
}
/**
* Class Constructor.
*
* @param id
* the id used to uniquely identify this object.
* @param dataTypeDbID
* the database id for {@link OmniText}
* @param activity
* the activity where this view item is to be built on
*/
public TextViewItem(int id, long dataTypeDbID, Activity activity) {
super(id, dataTypeDbID);
editText = new EditText(activity);
editText.setId(id);
}